function scale(h,point,mode)

%% Moves the selected waveform to the point "point" through a 
%% stretch depending on the initial state of the drag as captured in
%% the structure contrianed in the SelectionStruct property


if strcmp(mode,'motion')
    %% Capture the initial centroid and scaling params
    centroid = h.SelectionStruct.Centroid;   
    ydata = h.SelectionStruct.Selectedwave.Data.Amplitude;
    v = (point(2)-centroid)/(h.SelectionStruct.StartPoint-centroid);
    h.SelectionStruct.StartPoint = point(2);

    %% Modify the amplidude property without affecting the timeseries
    %% so that the transaction object is able to capture the complete scale once
    %% the buttonup event occurs
    h.SelectionStruct.Selectedwave.Data.Amplitude = (ydata-centroid)*v+centroid;

    
    %% Refresh - call draw on each wave to avoid triggering a viewchnage
    for k=1:length(h.waves)
       h.waves(k).RefreshMode = 'quick';
       h.waves(k).draw;
    end
    
    drawnow expose
    
elseif strcmp(mode,'complete')
    %% Get the transaction and recorder handles
    T = tsguis.transaction;
    recorder = tsguis.recorder;
    
    %% If the recorder is on cache the M code in the transaction buffer
    if strcmp(recorder.Recording,'on')
        thists = h.SelectionStruct.Selectedwave.DataSrc.Timeseries;
        c = h.SelectionStruct.Centroid;
        x = thists.Data(:);
        y = h.SelectionStruct.Selectedwave.Data.Amplitude(:);
        I = ~isnan(y);
        scalefact = (y(I)'*x(I))/(x(I)'*x(I));
        shift = mean(y(I)-scalefact*x(I));   
        T.addbuffer('%% Time series rescaling');
        T.addbuffer(sprintf('%s.Data = %s.Data*(%f)+%f;', thists.Name,thists.Name,...
            scalefact,c),thists);
    end

    %% During the drag all time series updates were routed to the @timedata
    %% Amplitlitude and Time, update the timeseries with these new values
    set(h.SelectionStruct.Selectedwave.DataSrc.Timeseries,'Data',...
        h.SelectionStruct.Selectedwave.Data.Amplitude,'Time',...
        h.SelectionStruct.Selectedwave.Data.Time)
    
    %% Clear old selections
    for k=1:length(h.Waves)
        set(h.Waves(k).View.Curves,'Selected','off')
    end
 
    %% Store transaction and clear watermark
    T.commit;
    recorder.pushundo(T);
        
    %% reset the refresh mode
    h.SelectionStruct.Selectedwave.RefreshMode = 'normal';
    
    %% Reset the selection struct and the mouse pointer  
    set(h.AxesGrid,'YLimMode',h.SelectionStruct.YLimMode,'XLimMode',...
        h.SelectionStruct.XLimMode)
    h.SelectionStruct = struct('Selectedwave',[],'StartPoint',[],...
        'Centroid',[],'XLimMode','','YLimMode',''); 
end   
