function mcodeDefaultConstructor(hObj,hCode)
% Default implementation for m-code generation object interface
% Called by MAKEMCODE, the code generator engine

% Copyright 2003-2004 The MathWorks, Inc.

% Generate helper functions
if isa(hObj,'hg.axes')
  localHGAxes_createConstructor(hObj,hCode);
elseif isa(hObj,'hg.surface')
  localHGSurface_createConstructor(hObj,hCode);
elseif isa(hObj,'hg.line')
  localHGLine_createConstructor(hObj,hCode);
elseif isa(hObj,'hg.image')
  localHGImage_createConstructor(hObj,hCode);
elseif isa(hObj,'hg.figure')
  localHGFigure_createConstructor(hObj,hCode);
end

generateDefaultPropValueSyntax(hCode); % method

%----------------------------------------------------------%
function localHGFigure_createConstructor(hObj,hCode)

% generate a call to 'figure'
% ToDo: This is specific to plot creation and will not be
% suitable for GUIDE applications.

%Generate:
%  colormap(...)
colormap_name = [];
cmap = get(hObj,'Colormap');
known_colormaps = {'jet', 'hsv', 'hot', 'gray', 'bone', 'copper', 'pink', 'white', 'flag', 'lines', 'colorcube',  'prism', 'cool', 'autumn', 'spring', 'winter', 'summer'};
for n = 1:length(known_colormaps) 
   if isequal(cmap, feval(known_colormaps{n}, length(cmap))) 
        if ~strcmp(known_colormaps{n},'jet')
            colormap_name = known_colormaps{n};
        end  
        break;
   end
end
if ~isempty(colormap_name)
   hArg = codegen.codeargument('Value',colormap_name);
   hFunc = codegen.codefunction('Name','colormap','CodeRef',hCode);
   addArgin(hFunc,hArg);
   addPostConstructorFunction(hCode,hFunc); 
end


%----------------------------------------------------------%
function localHGImage_createConstructor(hObj,hCode)

% Don't show xdata, ydata if auto-generated
% Determine if xdata and ydata were auto generated by
% image at constructor time. 

xdata = get(hObj,'XData');
ydata = get(hObj,'YData');
cdata = get(hObj,'CData');
m = size(cdata,1);
n = size(cdata,2);
if isequal(xdata,1:n) && isequal(ydata,1:m)
    ignoreProperty(hCode,{'XData','YData','CData'});
    
    % Force cdata to be first input argument of constructor
    hArg = codegen.codeargument('Name','CData','Value',cdata,'IsParameter',true);
    addConstructorArgin(hCode,hArg);
end

%----------------------------------------------------------%
function localHGLine_createConstructor(hObj,hCode)

ignoreProperty(hCode,{'XData','YData','ZData'}); 
    
xdata = get(hObj,'XData');
ydata = get(hObj,'YData');
zdata = get(hObj,'ZData');

% xdata
hArg = codegen.codeargument('Name','XData','Value',xdata,'IsParameter',true);
addConstructorArgin(hCode,hArg);

% ydata
hArg = codegen.codeargument('Name','YData','Value',ydata,'IsParameter',true);
addConstructorArgin(hCode,hArg);

% zdata
if ~isempty(zdata)
   hArg = codegen.codeargument('Name','ZData','Value',zdata,'IsParameter',true);
   addConstructorArgin(hCode,hArg);
end


%----------------------------------------------------------%
function localHGSurface_createConstructor(hObj,hCode)

% Don't show xdata, ydata if auto-generated
% Determine if xdata and ydata were auto generated by
% surface at constructor time. This is basically 
% reverse engineering how the surface constructor wrt
% how it handles zdata when doing: "surface(zdata)"

xdata = get(hObj,'xdata');
ydata = get(hObj,'ydata');
zdata = get(hObj,'zdata');
m = size(zdata,1);
n = size(zdata,2);

% surface('zdata',...)
if isequal(xdata,1:m) && isequal(ydata',1:n)
    ignoreProperty(hCode,{'XData','YData','ZData'});
    
    % Force zdata to be first input argument of constructor
    hArg = codegen.codeargument('Name','ZData','Value',zdata,'IsParameter',true);
    addConstructorArgin(hCode,hArg);
end

% Don't show VertexNormals if auto-generated
if strcmpi(get(hObj,'NormalMode'),'auto');
    ignoreProperty(hCode,{'VertexNormals'});
end

%----------------------------------------------------------%
function localHGAxes_createConstructor(hObj,hCode)
% Default implementation for axes.

setConstructorName(hCode,'axes'); % method

% If 'ActivePosition' property is 'Position', then don't generate
% code for 'OuterPosition' property.
if strcmpi(get(hObj,'ActivePositionProperty'),'Position')
    ignoreProperty(hCode,{'ActivePositionProperty','OuterPosition'});
    
% If 'ActivePosition' property is 'OuterPosition', then don't generate
% code for 'Position'. If, however, the axes was generated by the subplot
% command, then don't generated a call to 'OuterPosition' since the layout
% will be wrong -since 'LooseInsets' is not documented we can't generate 
% the proper code which will work for subplots. 
else
    hFig = handle(ancestor(hObj,'figure'));
    appdata = handle(getappdata(hFig,'SubplotGrid'));
    if any(hObj==appdata(:)) % if axes is part of a subplot
        ignoreProperty(hCode,{'ActivePositionProperty','OuterPosition'});  
    else
        ignoreProperty(hCode,{'ActivePositionProperty','Position'});
    end
end

% Generate code for 'View' or 'CameraPosition' but not both. This is 
% because the two properties are interdependent.
if hasProperty(hCode,'View') && hasProperty(hCode,'CameraPosition')
    ignoreProperty(hCode,'View');
end

localAddAxesHelperFunctions(hObj,hCode);

%----------------------------------------------------------%
function localAddAxesHelperFunctions(hObj,hCode)
% Generate post constructor helper functions for axes

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% axis(...), xlim(...), ylim(...), zlim(...)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ignoreProperty(hCode,{'xlim','ylim','zlim'});

% Get this object's state
is_xauto = strcmpi(get(hObj,'XLimMode'),'auto');
is_yauto = strcmpi(get(hObj,'YLimMode'),'auto');
is_zauto = strcmpi(get(hObj,'ZLimMode'),'auto');
xlm = get(hObj,'XLim');
ylm = get(hObj,'YLim');
zlm = get(hObj,'ZLim');

% Determine whether to call AXIS or XLIM, YLIM, ZLIM
fname = {};
fval = {};
if ~is_xauto && ~is_yauto && is_zauto  
     fname{1} = 'axis';
     fval{1} = [xlm,ylm];
elseif ~is_xauto && ~is_yauto && ~is_zauto 
     fname{1} = 'axis';
     fval{1} = [xlm,ylm,zlm];
else
   if ~is_xauto
     fname{end+1} = 'xlim';
     fval{end+1} = xlm;
   end
   if ~is_yauto
     fname{end+1} = 'ylim';
     fval{end+1} = ylm;
   end
   if ~is_zauto
     fname{end+1} = 'zlim';
     fval{end+1} = zlm;   
   end
end

% Loop through and create helper functions
for n = 1:length(fname)
   local_CreateAxesHelperFunction(hObj,hCode,fname{n},fval{n});
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% xlabel(...), ylabel(...), zlabel(...), title(...)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
props = {'title','xlabel','ylabel','zlabel'};
ignoreProperty(hCode,props); % method

% Loop through each label
for n = 1:length(props)
   labelname = props{n};
   
   % Get string for axes label
   htext = get(hObj,labelname);
   str = get(htext,'String');
   
   % if string isn't empty
   if ~isempty(str)
       local_CreateAxesHelperFunction(hObj,hCode,labelname,str) 
   end
end

%%%%%%%%%%%%%%%%
% view(axes,...)
%%%%%%%%%%%%%%%%
if hasProperty(hCode,'View')
   ignoreProperty(hCode,'View');
   local_CreateAxesHelperFunction(hObj,hCode,'view',get(hObj,'View'));
end

%%%%%%%%%%%%%%%%
% box(axes,...)
%%%%%%%%%%%%%%%%
if hasProperty(hCode,'Box')
   ignoreProperty(hCode,'Box');
   local_CreateAxesHelperFunction(hObj,hCode,'box',get(hObj,'Box'));
end

%%%%%%%%%%%%%%%%
% grid(axes,...)
%%%%%%%%%%%%%%%%
is_xgrid = strcmpi(get(hObj,'XGrid'),'on');
is_ygrid = strcmpi(get(hObj,'YGrid'),'on');
is_zgrid = strcmpi(get(hObj,'ZGrid'),'on');
if is_xgrid && is_ygrid && is_zgrid
   ignoreProperty(hCode,{'XGrid','YGrid','ZGrid'});
   local_CreateAxesHelperFunction(hObj,hCode,'grid','on');
end

do_hold_all = true;

% Is this a loglog or semilog plot? If so, don't generate call to
% hold all since that will prevent loglog from setting axis scale.
plot_list = plotchild(hObj);
is_2d_line_plot = ( is2D(hObj) && ...
                    isa(handle(plot_list),'graph2d.lineseries'));
if(is_2d_line_plot)
    is_logx = strcmpi(get(hObj,'XScale'),'log');
    is_logy = strcmpi(get(hObj,'YScale'),'log');
    if (is_logx || is_logy)
        ignoreProperty(hCode,{'XScale','YScale','XMinorTick','YMinorTick'});
        do_hold_all = false;
    end
end

%%%%%%%%%%%%%%%%%%
% hold(axes,'all')
%%%%%%%%%%%%%%%%%%
% Note: We must call hold(...) before the first plot is constructed 
% since some plots like surf.m will wipe out portions of the axes 
% state.
if (  do_hold_all && ...
      (strcmpi(get(hObj,'NextPlot'),'add') || length(plotchild(hObj))>0) ...
   )
    local_CreateAxesHelperFunction(hObj,hCode,'hold','all');
end
ignoreProperty(hCode,'NextPlot');
    
%---------------------------------------------------------%
function local_CreateAxesHelperFunction(hObj,hCode,fname,fval)
% Utility for creating axes helper functions in the form of:
%    fname(hAxes,fval);

% Create function object
hFunc = codegen.codefunction('Name',fname,'CodeRef',hCode);
addPostConstructorFunction(hCode,hFunc); % method
       
% Create first input argument: axes handle
hArg = codegen.codeargument('Value',hObj,'IsParameter',true); 
addArgin(hFunc,hArg); % method
   
% Create second input argument: string
hArg = codegen.codeargument('Value',fval);
addArgin(hFunc,hArg); % method



