function hndlgraf(action)
%HNDLGRAF Demonstrates Handle Graphics for line 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 line        
%   style of the plot, the line width, the marker size,
%   and the color using Handle Graphics.               
%                                                      
%   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.12 $  $Date: 2002/04/15 03:32:23 $

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

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

    figNumber=figure( ...
        'Visible','off', ...
        'NumberTitle','off', ...
        'Name','Handle Graphics and Line Objects');
    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','MiniCommand 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','hndlgraf(''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 LINE TYPE command popup button
    btnNumber=1;
    yLabelPos=top-(btnNumber-1)*(btnHt+labelHt+spacing);
    labelStr=' Line Type';
    labelList=' - | --| :| -.| o| x| +| *| .';
    cmdList=strvcat( ...
        ' ',' set(hndl,''LineStyle'',''--'')', ...
        ' set(hndl,''LineStyle'','':'')', ...
        ' set(hndl,''LineStyle'',''-.'')', ...
        ' set(hndl,''Marker'',''o'')', ...
        ' set(hndl,''Marker'',''x'')', ...
        ' set(hndl,''Marker'',''+'')', ...
        ' set(hndl,''Marker'',''*'')', ...
        ' set(hndl,''Marker'',''.'')');
    callbackStr='hndlgraf 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 LINE WIDTH command popup button
    btnNumber=2;
    yLabelPos=top-(btnNumber-1)*(btnHt+labelHt+spacing);
    labelStr=' Line Width';
    labelList=' 0.5|1|2|3|4';
    cmdList=strvcat( ...
        ' ',' set(hndl,''LineWidth'',1)', ...
        ' set(hndl,''LineWidth'',2)', ...
        ' set(hndl,''LineWidth'',3)', ...
        ' set(hndl,''LineWidth'',4)');
    callbackStr='hndlgraf 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 MARKER SIZE command popup button
    btnNumber=3;
    yLabelPos=top-(btnNumber-1)*(btnHt+labelHt+spacing);
    labelStr=' MarkerSize';
    labelList=' 6| 9| 12| 15| 18';
    cmdList=strvcat( ...
        ' ',' set(hndl,''MarkerSize'',9)', ...
        ' set(hndl,''MarkerSize'',12)', ...
        ' set(hndl,''MarkerSize'',15)', ...
        ' set(hndl,''MarkerSize'',18)');
    callbackStr='hndlgraf 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 COLOR command popup button
    btnNumber=4;
    yLabelPos=top-(btnNumber-1)*(btnHt+labelHt+spacing);
    labelStr=' Color';
    labelList=' cyan| red| green| blue| magenta| yellow';
    cmdList=strvcat( ...
        ' set(hndl,''Color'',''cyan'')', ...
        ' set(hndl,''Color'',''red'')', ...
        ' set(hndl,''Color'',''green'')', ...
        ' set(hndl,''Color'',''blue'')', ...
        ' set(hndl,''Color'',''magenta'')', ...
        ' set(hndl,''Color'',''yellow'')', ...
        ' set(hndl,''Color'',''white'')');
    callbackStr='hndlgraf 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','hndlgraf(''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);
    hndlgraf('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=strvcat(' x=0:0.02:1;',' hndl=plot(x,humps(x));');
    for count=2:n,
        newStrList=get(hndlList(count),'UserData');
        newStrVal=get(hndlList(count),'Value');
        newStr=deblank(newStrList(newStrVal,:));
        if ~isempty(newStr), evalStr=strvcat(evalStr,newStr); end;
    end;
    set(mcwHndl,'String',evalStr);
    evalmcw(mcwHndl);

end;    % if strcmp(action, ...
