function varargout = guideopts(varargin)
% GUIDEOPTS
% OPTIONS = GUIDEOPTS(FIG) return the options struct on fig
% GUIDEOPTS(FIG, OPTIONS) set the options struct on fig
% GUIDEOPTS(FIG, 'edit') display the GUIDE Application Options
% dialog on fig
%
% Stores a struct in the GUIDE proxy figure's ApplicationData
% under the name 'GUIDEOptions', with the following fields:
%  options.resize      = [ 'none' | 'simple' | 'custom' ] (omResize)
%  options.access      = [ 'on' | 'off' | 'custom' ]      (omAccessibility)
%  options.mfile       = [ 1 | 0 ]                        (rbFigOnly,rbFigM)
%  options.callbacks   = [ 1 | 0 ]                        (cbCallbacks)
%  options.singleton   = [ 1 | 0 ]                        (cbSingleton)
%  options.blocking    = [ 1 | 0 ]                        (cbBlocking)
%  options.syscolorfig = [ 1 | 0 ]                        (cbBackground)
%
%  pbOk, pbHelp
%
% Application MFile generated by GUIDE Application Development Assistant v1.0
% (Damian to add boilerplate help entry here)
% (Also add an indication that this was auto-generated)

%   $Revision: 1.8 $  $Date: 2000/08/31 20:50:43 $

if nargin == 0
  % RAISE EXISTING WINDOW, OR OPEN NEW ONE (ONLY ONE ALLOWED)
  fig = openfig(mfilename, 'reuse');

  % Use system color scheme for figure:
  set(fig,'color',get(0,'defaultUicontrolBackgroundColor'));

  guidata(fig, guihandles(fig));
  
  varargout{1} = fig;
  
elseif ischar(varargin{1})
  %%
  %% CALL THE CALLBACK SUBFUNCTION:
  %%
  [varargout{1:nargout}] = feval(varargin{:});
elseif ishandle(varargin{1})
  fig = varargin{1};
  switch nargin
   case 1
    varargout{1} = getOptions(fig);
   case 2
    if isstruct(varargin{2})
      setOptions(fig, varargin{2});
    elseif isequal(varargin{2}, 'edit')
      editOptions(fig);
    else
      error('second input argument must be an options structure or ''edit''');
    end
   otherwise
    error('too many input arguments');
  end
end

function pbHelp_Callback(h, eventdata, handles, varargin)
helpview([docroot '/mapfiles/creating_guis.map'],...
	 'configuring_application_options')

function pbOk_Callback(h, eventdata, handles, varargin)
close(handles.figure1);

function omAccessibility_Callback(h, eventdata, handles, varargin)
options = guideopts(handles.guideFig);
vals = get(h,'userdata');
options.accessibility = vals{get(h,'value')};
guideopts(handles.guideFig, options);
handleIndex = find(allchild(0) == handles.guideFig);
setAccessibility(handles.guideFig, options.accessibility);
newHandles = allchild(0);
handles.guideFig = newHandles(handleIndex);
setappdata(handles.dialogFig, 'handles', handles);

function omResize_Callback(h, eventdata, handles, varargin)
options = guideopts(handles.guideFig);
vals = get(h,'userdata');
options.resize = vals{get(h,'value')};
setResizePolicy(handles.guideFig, options.resize);
guideopts(handles.guideFig, options);

function cbBlocking_Callback(h, eventdata, handles, varargin)
options = guideopts(handles.guideFig);
options.blocking = get(h,'value');
guideopts(handles.guideFig, options);

function cbBackground_Callback(h, eventdata, handles, varargin)
options = guideopts(handles.guideFig);
options.syscolorfig = get(h,'value');
if options.syscolorfig
  set(handles.guideFig,...
      'color', get(0, 'defaultuicontrolbackgroundcolor'));
end
guideopts(handles.guideFig, options);

function cbSingleton_Callback(h, eventdata, handles, varargin)
options = guideopts(handles.guideFig);
options.singleton = get(h,'value');
guideopts(handles.guideFig, options);

function cbCallbacks_Callback(h, eventdata, handles, varargin)
options = guideopts(handles.guideFig);
options.callbacks = get(h,'value');
guideopts(handles.guideFig, options);
setCallbackPolicy(handles.guideFig);

function rbFigM_Callback(h, eventdata, handles, varargin)
options = guideopts(handles.guideFig);
options.mfile = 1;
set([handles.rbFigOnly handles.rbFigM],{'value'},{0; 1})
enab = [handles.cbSingleton,
	handles.cbCallbacks,
	handles.cbBackground,
	handles.cbBlocking];
if isnan(options.singleton)
  enab(1) = [];
end
set(enab, 'enable','on');
guideopts(handles.guideFig, options);
setCallbackPolicy(handles.guideFig);

function rbFigOnly_Callback(h, eventdata, handles, varargin)
set([handles.rbFigOnly handles.rbFigM],{'value'},{1; 0})
set([handles.cbSingleton,
     handles.cbCallbacks,
     handles.cbBackground,
     handles.cbBlocking], 'enable','off');
options = guideopts(handles.guideFig);
options.mfile = 0;
guideopts(handles.guideFig, options);
setCallbackPolicy(handles.guideFig);

function options = getOptions(fig)
options = getappdata(fig, 'GUIDEOptions');
  
% data for GUIDE internal use:
if ~isfield(options,'active_h'), options.active_h = [];  end
if ~isfield(options,'taginfo'), options.taginfo = [];   end
if ~isfield(options,'override'), options.override = 0; end
% user preferences not related to M-file:
if ~isfield(options,'resize'), options.resize = getResizePolicy(fig);end
if ~isfield(options,'accessibility'), options.accessibility = getAccessibility(fig);end
% user preferences related to M-file:
if ~isfield(options,'mfile'), options.mfile = 1;      end
if ~isfield(options,'callbacks'), options.callbacks = 1;  end
if ~isfield(options,'singleton'), options.singleton = 1;  end
if ~isfield(options,'blocking'), options.blocking = 0;   end
if ~isfield(options,'syscolorfig'), options.syscolorfig = 1;end

guideopts(fig, options); % don't forget to store newly created opts!

%---------------------------------------------------------------
function setOptions(fig, options)
setappdata(fig, 'GUIDEOptions', options);

function setCallbackPolicy(fig)
options = getOptions(fig);
if options.mfile & options.callbacks
  guidemfile('chooseAutoCallbacks',findall(fig));
else
  guidemfile('loseAutoCallbacks',findall(fig));
end

function r = getResizePolicy(fig)
%
% 'none', 'simple', or 'custom'
%
if strcmp(get(fig,'resize'),'off')
  r = 'none';
else
  kids = allchild(fig);
  kids = [findobj(kids,'flat','type','uicontrol')
	  findobj(kids,'flat','type','axes')];
  u = get(kids,'units');
  if length(strmatch('normalized',u)) == length(kids)
    r = 'simple';
  else
    r = 'custom';
  end
end

function setResizePolicy(fig, r)
switch r
 case 'none'
  set(fig,'resize','off');
  kids = allchild(fig);
  kids = [findobj(kids,'flat','type','uicontrol')
	  findobj(kids,'flat','type','axes')];
  set(kids,'units','characters');
 case 'simple'
  set(fig,'resize','on');
  kids = allchild(fig);
  kids = [findobj(kids,'flat','type','uicontrol')
	  findobj(kids,'flat','type','axes')];
  set(kids,'units','normalized');
 case 'custom'
  set(fig,'resize','on');
  options = guideopts(fig);
  if    options.mfile & ...
	options.callbacks & ...
	isempty(get(fig, 'resizefcn'))
    guidemfile('setAutoCallback',fig,'resizefcn');
  end
    
 otherwise
  error(['unknown resize policy: ',r])
end

function p = getAccessibility(fig)
%
% 'on', 'off', or 'custom'
%
switch [get(fig, 'integerhandle') get(fig, 'handlevisibility')]
 case 'onon'
  p = 'on';
 case 'offoff'
  p = 'off';
 otherwise
  p = 'custom';
end

function setAccessibility(fig, p)
switch p
 case 'on'
  set(fig, 'IntegerHandle', 'on', 'HandleVisibility', 'on');
 case 'off'
  set(fig, 'IntegerHandle', 'off', 'HandleVisibility', 'off');
end

function editOptions(fig)
% initialize and display the dialog box, and block until it is dismissed
guideFig = fig;
dialogFig = guideopts;
handles = guidata(dialogFig);
handles.guideFig = guideFig;
handles.dialogFig = dialogFig;
saveWhenDone = 0;

options = guideopts(guideFig);
filename = get(guideFig, 'filename');
if ~isempty(filename)
  mname = [filename(1:end-3) 'm'];
  if exist(mname)
    saveWhenDone = 1;
    mopts = guidemfile('getOptions', mname)
    if isfield(mopts, 'singleton')
      options.singleton = mopts.singleton;
      set(handles.cbSingleton, 'enable','on');
    else
      options.singleton = nan;
      set(handles.cbSingleton, 'enable','off');
    end
    if isfield(mopts, 'blocking')
      options.blocking = mopts.blocking;
    end
    if isfield(mopts, 'syscolorfig')
      options.syscolorfig = mopts.syscolorfig;
    end
    guideopts(guideFig, options);
  end
end

set(handles.omResize,...
    'value',...
    strmatch(options.resize, get(handles.omResize,'userdata')));
set(handles.omAccessibility,...
    'value',...
    strmatch(options.accessibility, get(handles.omAccessibility,'userdata')));

if options.mfile
  on = handles.rbFigM;
else
  on = handles.rbFigOnly;
  set([handles.cbSingleton,
       handles.cbCallbacks,
       handles.cbBlocking,
       handles.cbBackground],'enable','off');
end
set(on, 'value', 1);

on = [];
if(options.callbacks),           on(end + 1) = handles.cbCallbacks;  end
% use isequal because it might be NaN
if isequal(options.singleton, 1),on(end + 1) = handles.cbSingleton;  end
if(options.syscolorfig),         on(end + 1) = handles.cbBackground; end
if(options.blocking),            on(end + 1) = handles.cbBlocking;   end
set(on, 'value', 1);

guidata(dialogFig, handles);

waitfor(dialogFig);

% update the MFile based on new options:
if saveWhenDone
  options = guideopts(fig);
  % this signals SAVE to honor the new properties in options rather
  % than to honor the existing code in the Mfile
  options.override = 1;
  guideopts(fig, options);
  guidefunc('save', guideFig);
end
