function graf3d(action)
%GRAF3D Demonstrate Handle Graphics for surface plots in MATLAB.
%   This window allows you to assemble a string                    
%   of MATLAB commands that results in a plot in                   
%   the axis in the upper left corner.                             
%                                                                  
%   By playing with the popup menus on the right                   
%   side of the window, you can adjust the type                    
%   of plot, the type of shading, the color map,                   
%   and so on.                                                     
%                                                                  
%   The MiniCommand Window in the lower                            
%   right shows the list of commands that create                   
%   the plot. If you like, you can even directly edit              
%   the commands in the MiniCommand Window.                        
%   Type control-return to execute code in the                     
%   MiniCommand Window.                                            
                                                                
%   Ned Gulley, 6-21-93
%   Copyright 1984-2002 The MathWorks, Inc. 
%   $Revision: 5.13 $  $Date: 2002/04/15 03:31:55 $

if nargin<1,
    action='initialize';
end;

if strcmp(action,'initialize'),
    oldFigNumber=watchon;

    figNumber=figure( ...
        'Visible','off', ...
        'NumberTitle','off', ...
        'Color','black', ...
        'Name','3-D Plots in Handle Graphics');
    colordef(figNumber,'none')
    axes( ...
        'Units','normalized', ...
        'Position',[0.07 0.45 0.60 0.50]);

    %===================================
    % Set up the MiniCommand Window
    top=0.35;
    left=0.05;
    right=0.70;
    bottom=0.05;
    labelHt=0.05;
    spacing=0.005;
    % First, the MiniCommand Window frame
    frmBorder=0.02;
    frmPos=[left-frmBorder bottom-frmBorder ...
        (right-left)+2*frmBorder (top-bottom)+2*frmBorder];
    uicontrol( ...
        'Style','frame', ...
        'Units','normalized', ...
        'Position',frmPos, ...
        'BackgroundColor',[0.50 0.50 0.50]);
    % Then the text label
    labelPos=[left top-labelHt (right-left) labelHt];
    uicontrol( ...
        'Style','text', ...
        'Units','normalized', ...
        'Position',labelPos, ...
        'BackgroundColor',[0.50 0.50 0.50], ...
        'ForegroundColor',[1 1 1], ...
        'String','Command Window');
    % Then the editable text field
    mcwPos=[left bottom (right-left) top-bottom-labelHt-spacing];
    mcwHndl=uicontrol( ...
        'Style','edit', ...
        'HorizontalAlignment','left', ...
        'Units','normalized', ...
        'Max',10, ...
        'BackgroundColor',[1 1 1], ...
        'Position',mcwPos, ...
        'Callback','graf3d(''quickeval'')', ...
        'String',' % Press buttons at the right for example plots.');
    % Save this handle for future use
    set(gcf,'UserData',mcwHndl);

    %====================================
    % Information for all buttons
    labelColor=[0.8 0.8 0.8];
    top=0.95;
    bottom=0.05;
    left=0.75;
    yInitLabelPos=0.90;
    left=0.75;
    labelWid=0.20;
    labelHt=0.05;
    btnWid=0.20;
    btnHt=0.05;
    % Spacing between the label and the button for the same command
    btnOffset=0.003;
    % Spacing between the button and the next command's label
    spacing=0.05;

    %====================================
    % The CONSOLE frame
    frmBorder=0.02;
    yPos=0.05-frmBorder;
    frmPos=[left-frmBorder yPos btnWid+2*frmBorder 0.9+2*frmBorder];
    h=uicontrol( ...
        'Style','frame', ...
        'Units','normalized', ...
        'Position',frmPos, ...
        'BackgroundColor',[0.50 0.50 0.50]);
    
    %====================================
    % The PLOT TYPE command popup button
    btnNumber=1;
    yLabelPos=top-(btnNumber-1)*(btnHt+labelHt+spacing);
    labelStr=' Plot Type';
    labelList=' surf| surfl| mesh| meshz| waterfall| pcolor';
    cmdList=str2mat( ...
        ' surf(z)',' surfl(z)',' mesh(z)', ...
        ' meshz(z)',' waterfall(z)',' pcolor(z)');
    callbackStr='graf3d eval';
    
    % Generic label information
    labelPos=[left yLabelPos-labelHt labelWid labelHt];
    uicontrol( ...
        'Style','text', ...
        'Units','normalized', ...
        'Position',labelPos, ...
        'BackgroundColor',labelColor, ...
        'HorizontalAlignment','left', ...
        'String',labelStr);

    % Generic popup button information
    btnPos=[left yLabelPos-labelHt-btnHt-btnOffset btnWid btnHt];
    hndl1=uicontrol( ...
        'Style','popup', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelList, ...
        'Callback',callbackStr, ...
        'UserData',cmdList);

    %====================================
    % The SHADING command popup button
    btnNumber=2;
    yLabelPos=top-(btnNumber-1)*(btnHt+labelHt+spacing);
    labelStr=' Shading';
    labelList=' faceted| flat| interp';
    cmdList=str2mat( ...
        ' ',' shading flat',' shading interp');
    callbackStr='graf3d eval';
    
    % Generic label information
    labelPos=[left yLabelPos-labelHt labelWid labelHt];
    uicontrol( ...
        'Style','text', ...
        'Units','normalized', ...
        'Position',labelPos, ...
        'BackgroundColor',labelColor, ...
        'HorizontalAlignment','left', ...
        'String',labelStr);

    % Generic popup button information
    btnPos=[left yLabelPos-labelHt-btnHt-btnOffset btnWid btnHt];
    hndl2=uicontrol( ...
        'Style','popup', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelList, ...
        'Callback',callbackStr, ...
        'UserData',cmdList);

    %====================================
    % The COLORMAP command popup button
    btnNumber=3;
    yLabelPos=top-(btnNumber-1)*(btnHt+labelHt+spacing);
    labelStr=' Colormap';
    labelList=' hsv| jet| cool| hot| gray| pink| copper';
    cmdList=str2mat( ...
        ' colormap(hsv)',' colormap(jet)',' colormap(cool)',' colormap(hot)', ...
        ' colormap(gray)',' colormap(pink)',' colormap(copper)');
    callbackStr='graf3d eval';
    
    % Generic label information
    labelPos=[left yLabelPos-labelHt labelWid labelHt];
    uicontrol( ...
        'Style','text', ...
        'Units','normalized', ...
        'Position',labelPos, ...
        'BackgroundColor',labelColor, ...
        'HorizontalAlignment','left', ...
        'String',labelStr);

    % Generic popup button information
    btnPos=[left yLabelPos-labelHt-btnHt-btnOffset btnWid btnHt];
    hndl3=uicontrol( ...
        'Style','popup', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelList, ...
        'Callback',callbackStr, ...
        'UserData',cmdList);

    %====================================
    % The AXIS command popup button
    btnNumber=4;
    yLabelPos=top-(btnNumber-1)*(btnHt+labelHt+spacing);
    labelStr=' Axis';
    labelList=' on| off| ij| xy';
    cmdList=str2mat( ...
        ' ',' axis off',' axis ij',' axis xy');
    callbackStr='graf3d eval';
    
    % Generic label information
    labelPos=[left yLabelPos-labelHt labelWid labelHt];
    uicontrol( ...
        'Style','text', ...
        'Units','normalized', ...
        'Position',labelPos, ...
        'BackgroundColor',labelColor, ...
        'HorizontalAlignment','left', ...
        'String',labelStr);

    % Generic popup button information
    btnPos=[left yLabelPos-labelHt-btnHt-btnOffset btnWid btnHt];
    hndl4=uicontrol( ...
        'Style','popup', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelList, ...
        'Callback',callbackStr, ...
        'UserData',cmdList);

    %====================================
    uicontrol( ...
        'Style','pushbutton', ...
        'Units','normalized', ...
        'Position',[left bottom+2*btnHt+spacing btnWid 2*btnHt], ...
        'String','Info', ...
        'Callback','graf3d(''info'')');

    % The close button.
    uicontrol( ...
        'Style','pushbutton', ...
        'Units','normalized', ...
        'Position',[left bottom btnWid 2*btnHt], ...
        'String','Close', ...
        'Callback','close(gcf)');

    % Uncover the figure
    hndlList=[mcwHndl hndl1 hndl2 hndl3 hndl4];
    watchoff(oldFigNumber);
    set(figNumber,'Visible','on', ...
        'UserData',hndlList);
    graf3d('eval');

elseif strcmp(action,'info'),
    helpwin(mfilename)
   
elseif strcmp(action,'quickeval'),
    % Execute the MiniCommand Window text exactly as it is now.
    hndlList=get(gcf,'UserData');
    mcwHndl=hndlList(1);
    evalmcw(mcwHndl);

elseif strcmp(action,'eval'),
    % Assemble and execute the completed command 
    hndlList=get(gcf,'UserData');
    n=length(hndlList);
    mcwHndl=hndlList(1);
    evalStr=str2mat(' z=peaks(25);');
    for count=2:n,
        newStrList=get(hndlList(count),'UserData');
        newStrVal=get(hndlList(count),'Value');
        newStr=deblank(newStrList(newStrVal,:));
        if ~isempty(newStr), evalStr=str2mat(evalStr,newStr); end;
    end;
    set(mcwHndl,'String',evalStr);
    evalmcw(mcwHndl);

end;    % if strcmp(action, ...
