function [] = cpl_finish_crop_drag()
%
% cpl_finish_crop_drag.m--Callback to be invoked when one of the crop lines
% is dragged and released.
%
% Syntax: cpl_finish_crop_drag
% Developed in Matlab 7.0.1.24704 (R14) Service Pack 1 on GLNX86.
% Kevin Bartlett (kpb@uvic.ca), 2006-03-20 15:35
%-------------------------------------------------------------------------
thisLine = gco;
figNum = gcbf;
thisLineName = get(thisLine,'Tag');
%cpl_constants = get(gcbf,'userdata');
cpl_properties = getappdata(figNum,'cpl_properties');
% Use the line's tag to find out if it is a start line or an end line.
START = 0;
END = 1;
if strfind(thisLineName,'StartLine')
lineType = START;
else
lineType = END;
end % if
% Do not allow a crop line to be dragged outside the plot limits.
thisLineXVal = unique(get(thisLine,'xdata'));
xlims = get(findobj(figNum,'Tag','full_ax'),'xlim');
if thisLineXVal < min(xlims)
thisLineXVal = min(xlims);
end % if
if thisLineXVal > max(xlims)
thisLineXVal = max(xlims);
end % if
% Do not allow a start line to be dragged to after an end line, and vice
% versa.
oldStartXVal = unique(get(findobj(figNum,'Tag','full_ax_StartLine'),'xdata'));
oldEndXVal = unique(get(findobj(figNum,'Tag','full_ax_EndLine'),'xdata'));
xIncrement = cpl_properties.x(2) - cpl_properties.x(1);
if lineType == START
if thisLineXVal >= oldEndXVal
thisLineXVal = oldEndXVal - xIncrement/2;
end % if
else
if thisLineXVal <= oldStartXVal
thisLineXVal = oldStartXVal + xIncrement/2;
end % if
end % if
% Move all other lines of this type to the same x-value.
if lineType == START
thisTypeLines = [findobj(figNum,'Tag','full_ax_StartLine') ...
findobj(figNum,'Tag','start_ax_StartLine') ...
findobj(figNum,'Tag','end_ax_StartLine')];
else
thisTypeLines = [findobj(figNum,'Tag','full_ax_EndLine') ...
findobj(figNum,'Tag','start_ax_EndLine') ...
findobj(figNum,'Tag','end_ax_EndLine')];
end % if
set(thisTypeLines,'xdata',[thisLineXVal thisLineXVal]);
% Find the index to the data corresponding to the crop line's new position.
%[dummy,minIndex] = min(abs(thisLineXVal - cpl_constants.x));
if lineType == START
%cpl_constants.startIndex = min(find(cpl_constants.x >= thisLineXVal));
cpl_properties.startIndex = min(find(cpl_properties.x >= thisLineXVal));
else
%cpl_constants.endIndex = max(find(cpl_constants.x <= thisLineXVal));
cpl_properties.endIndex = max(find(cpl_properties.x <= thisLineXVal));
end % if
%set(gcbf,'userdata',cpl_constants);
setappdata(figNum,'cpl_properties',cpl_properties);
cpl_update(figNum);