function varargout=guide(varargin)
%GUIDE  Open the GUI Design Environment.

%   GUIDE will be removed in a future release. Use APPDESIGNER instead.

%   GUIDE initiates the GUI Design Environment(GUIDE) tools that allow GUIs
%   to be created or edited interactively from FIG-files or handle(s) to
%   figure.
%
%   GUIDE(filename) opens the FIG-file named 'filename' for editing if it
%   is on the MATLAB path. GUIDE(fullpath) opens the FIG-file at 'fullpath'
%   even if it is not on the MATLAB path.
%
%   GUIDE(HandleList) opens the content of each of the figures in
%   HandleList in a separate copy of the GUIDE design environment.
%
%   See also INSPECT.

%   GUIDE(..., '-test') allows internal MathWorks automated tests to run.

%   Copyright 1984-2020 The MathWorks, Inc.

import matlab.internal.lang.capability.Capability;
Capability.require([Capability.Swing, Capability.ComplexSwing]);

if nargin == 0
    openBlankApp();
    return;
end

% Turn off the callstack for warnings. By default the warning will include the callstack.
w = warning('off', 'backtrace');

% When the GUIDE Design Environment is opened, show a warning.
warning(message('MATLAB:guide:GUIDEWillBeRemovedUseAppDesigner'))

% Restore warning state
warning(w);

if nargin > 0
    [varargin{:}] = convertStringsToChars(varargin{:});
end


% error out if there is insufficient java support on this platform
errormsg = javachk('swing', 'GUIDE');
if ~isempty(errormsg)
    error(message('MATLAB:guide:NoSwingSupport', errormsg.message));
end

locNargin = nargin;
if nargin
    if (isequal(varargin{end}, '-test'))
        setappdata(0, 'MathWorks_GUIDE_testmode',1);
        locNargin = locNargin - 1;
    elseif (isequal(varargin{end}, '-testsave'))
        setappdata(0, 'MathWorks_GUIDE_testmode',2);
        locNargin = locNargin - 1;
    else
        if isappdata(0,'MathWorks_GUIDE_testmode')
            rmappdata(0,'MathWorks_GUIDE_testmode');
        end
    end
else
    if isappdata(0,'MathWorks_GUIDE_testmode')
        rmappdata(0,'MathWorks_GUIDE_testmode');
    end
end

% Guarantee no more than one arguments left after handling
% arguments by previous checking -test and -testsave options
if locNargin > 1
    error(message('MATLAB:guide:TooManyInputs'));
end

% This chunk of code initializes filename to:
%  - an empty string  (blank gui)
%  - a valid filename (open it)
%  - zero             (cancel)l
if getappdata(0, 'MathWorks_GUIDE_testmode')
    if (locNargin>0 && ischar(varargin{1}))
        filename = varargin{1};
    else
        filename = '';
    end
elseif locNargin == 0
    if nargout==0
        % When output is not expected, guidetemplate will use the startup
        % optimization by creating an extra invisible LayoutEditor. It will
        % then call guide.m again with the user selection in the
        % QuickStartDialog
        guidetemplate;
        return;
    else
        % When output is expected, guidetemplate will not use the startup
        % optimization. It returns an empty string or a legit filename on
        % success. If the result is zero, the user hit cancel.
        filename = guidetemplate;
        if isequal(filename,0)
            varargout{1} = [];
            return;
        elseif ishandle(filename)
            varargin{1} = filename;
        else
            % filename should be a char array
        end
    end
elseif ischar(varargin{1})
    % open specified filename
    filename = varargin{1};
else
    % set filename to zero for testing later
    filename = 0;
end

% We want to pass a filename with a fully specified path to the java code.
% Be prepared to handle two other cases
%
% 1) a file name specified w/o a .fig extension
%   (just append it)
% 2) a file name specified w/o a path
%   (prepend the path by using WHICH to find the file in the current
%    directory or on the path)
%
% After prepending the path and/or apending the extension, use EXIST to
% confirm that it is a legitimate file.

% handle the special case of guide(0) which was legal in R11
if isempty(filename) || (locNargin>0 && isequal(varargin{1}, 0))
    % Open an untitled layout editor.
    result = com.mathworks.toolbox.matlab.guide.LayoutEditor.newLayoutEditor;
elseif ischar(filename)
    [path,file,ext] = fileparts(filename);
    
    % Append the .fig extension if necessary
    if isempty(ext), ext = '.fig'; end
    
    if ~strcmp(ext, '.fig')
        error(message('MATLAB:guide:InvalidFileExtension', filename, '.fig'));
    end
    
    % Try to open a fig file.
    filename = fullfile(path, [file ext]);
    
    % Prepend the path if necessary
    savedname = filename;
    if isempty(path)
        filename = which(filename);
    end
    
    % Make sure it exists before opening the layout editor
    if ~exist(filename, 'file')
        error(message('MATLAB:guide:InvalidFileName', savedname));
    end
    
    result = com.mathworks.toolbox.matlab.guide.LayoutEditor.openLayoutEditor(filename);
else
    % Try to clone figures based on given handles.
    % shape input argument into a row vector
    figs = varargin{1}; figs = figs(:)';
    
    % verify that they are all figure handles
    if any(~ishandle(figs)) ||...
            length(figs) ~= length(strmatch('figure',get(figs,'type'),'exact'))
        error(message('MATLAB:guide:InvalidFigureHandle'));
    end
    
    result = cell(length(figs));
    for i=1:length(figs)
        result{i} = com.mathworks.toolbox.matlab.guide.LayoutEditor.openLayoutEditor(double(figs(i)));
    end
    
    % XXX what is the definition of "result"?
    result = result{1};
end

if nargout
    varargout{1} = result;
end

function openBlankApp()

tplfile = fullfile(matlabroot,'toolbox','matlab','guide','guitemplates','guidetemplate0');
sourceMATLABFile = [tplfile,'.m'];
sourceFIGFile = [tplfile,'.fig'];

% Copy the golden blank template app to writable temporary GUIDE Files.
temp = tempname;
targetMATLABFile = [temp, '.m'];
targetFIGFile = [temp, '.fig'];

copyfile(sourceFIGFile, targetFIGFile, 'writable');
fileattrib(targetFIGFile, '+w');
copyfile(sourceMATLABFile, targetMATLABFile,'writable');
fileattrib(targetMATLABFile, '+w');

% Set templateFile and templateFileSave in the appdata of
% groot.  This follows the existing recipe.  The appdata is
% reset to default values during the GUIDE app opening process.
setappdata(0,'templateFile', sourceFIGFile);
setappdata(0,'templateFileSave', 0);

% Open the target GUIDE file in GUIDE.
guide(targetFIGFile)