function moveaxis(arg)
%MOVEAXIS Used to grab and move legend axis.
%   To use, click and hold down a mouse button while
%   the cursor is near the lower left corner of the
%   axis you want to move. Wait for the cursor to change
%   to a fleur (4 way arrows), then drag the legend or axis
%   to the desired location and release the mouse button.
%
%   To enable, set the figure window's WINDOWBUTTONDOWNFCN
%   property to 'moveaxis'. For example,
%
%            set(gcf,'WindowButtonDownFcn','moveaxis')

%   10/5/93  D.Thomas
%   Copyright (c) 1984-98 by The MathWorks, Inc.
%   $Revision: 5.10 $  $Date: 1998/05/12 22:40:34 $

global OLDCA DELTA HL FIGUTS;
if nargin==0,
    fig = gcf;
    st=get(fig,'SelectionType');
    if (strcmp(st,'normal'))
        moveaxis(1);
    elseif strcmp(st,'open'),
        if strcmp(get(gco,'Type'),'text') & ...
             strcmp(get(get(gco,'parent'),'tag'),'legend')
            legend('EditLegend',gco)
        end
    end
elseif arg==1,
    fig = gcf;
    FIGUTS = get(fig,'units');
    set(fig,'pointer','fleur');
    if strcmp(FIGUTS,'normalized'),
        pnt = get(fig,'currentpoint');
        set(fig,'units','pixels');  
        pos = get(fig,'position');
        pnt = [pnt(1) * pos(3) pnt(2) * pos(4)];
    else,
        set(fig,'units','pixels');
        pnt=get(fig,'currentpoint');
    end
    Kids=get(fig,'children');
    [n,tmp]=size(Kids);
    mn=1e20;
    mi=1;
    for i=1:n,
        if strcmp(get(Kids(i),'type'),'axes') & ...
           strcmp(get(Kids(i),'tag'),'legend')
            units=get(Kids(i),'units');
            set(Kids(i),'units','pixels')
            cap=get(Kids(i),'position');
            if sum((pnt-cap(1:2)).^2)<mn,
                mn=sum((pnt-cap(1:2)).^2);
                mi=i;
                DELTA=cap(1:2)-pnt;
            end
            set(Kids(i),'units',units);
        end
    end
    ud = get(Kids(mi),'userdata');
    OLDCA=gca;  
    set(fig,'currentobject',Kids(mi));          
    HL=[Kids(mi) abs(get(Kids(mi),'units'))];
    set(Kids(mi),'units','pixels');
    set(fig,'windowbuttonmotionfcn','moveaxis(2)')
    set(fig,'windowbuttonupfcn','moveaxis(3)');
elseif arg==2,
    fig = gcf;
    pos=get(get(fig,'currentobject'),'position');
    set(get(fig,'currentobject'),'units','pixels','drawmode','fast',...
    'position',[get(fig,'currentpoint')+DELTA pos(3:4)]);
elseif arg==3,
    fig = gcf;
    set(fig,'WindowButtonMotionfcn','', ...
        'pointer','arrow','currentaxes',OLDCA, ...
        'windowbuttonupfcn','');
    set(HL(1),'units',setstr(HL(2:length(HL))));
    set(fig,'units',FIGUTS);
    if strcmp(get(gca,'tag'),'legend')
      legend('ShowLegendPlot')
    end
end
