function [han] = clabel_tool(cs, hs)

% Tool for manipulting the labels on a contour plot.
% function [han] = clabel_tool(cs, hs)
% cs and hs are the arrays returned by the call to contour, i.e., from
%   [cs, hs] = contour(x, y, z)
% or similar. han is the vector of handles to the labels.
%
% Calling clabel_tool causes a menu to pop up that includes the ability to
% add and delete labels and to specify the number of significant figures in
% the labels. Note that the labels are made with calls to clabel_jvm which
% which is a slightly debugged version of matlabs clabel.m.

% $Id: clabel_tool.m,v 1.3 1998/08/20 01:37:03 mansbrid Exp $
% Copyright J. V. Mansbridge, CSIRO, Mon Aug 17 16:36:10 EST 1998

if nargin ~= 2
  error('clabel_tool must be passed cs and hs');
end

% Store all of the x-y co-ordinates of each line.

len_hs = length(hs);
for ii = 1:len_hs
  cdat{ii} = get(hs(ii), 'CData');
  fdat{ii} = get(hs(ii), 'FaceVertexCData');
  xdat{ii} = get(hs(ii), 'XData');
  ydat{ii} = get(hs(ii), 'YData');
end

% Identify all of the labels already on the contours.

ax = axis;
hh_tot = get(gca, 'Children');
hh = findobj(hh_tot, 'Type', 'text');

% Now make the required alterations.

while(1)
  choice = menu('Choose a labelling option', ...
      'automatic labels (at all levels)', ...
      'automatic labels (at chosen levels)', ...
      'add many labels using mouse (dodgy)', ...
      'delete one label using mouse', ...
      'delete many labels using mouse', ...
      'delete all labels', ...
      'specify # of significant figures in all labels', ...
      'make all negative contours dashed', ...
      'exit');
  switch choice
    case 1 % 'automatic labels (at all levels)'
      hh = [hh; clabel_jvm(cs, hs)];
      length(hh)
    case 2 % 'automatic labels (at chosen levels)'
      v = input('levels to be labelled (e.g. 1:4 or [2 5 7]: ')
      hh = [hh; clabel_jvm(cs, hs, v)];
    case 3 % 'add many labels using mouse (dodgy)'
      hh = [hh; clabel_jvm(cs, hs, 'manual')];
    case 4 % 'delete one label using mouse'
      [x, y] = ginput(1);
      
      % Find the line segment closest to the mouse click and refresh it. This
      % will result in all of the other labels in that line segment having the
      % line drawn through them.

      vec_mag = repmat(NaN, len_hs, 1);
      for ii = 1:len_hs
	vec_mag(ii) = min((x - xdat{ii}).^2 + (y - ydat{ii}).^2);
      end
      [junk, han_cont] = min(vec_mag);
      
      set(hs(han_cont), 'CData', cdat{han_cont});
      set(hs(han_cont), 'FaceVertexCData', fdat{han_cont});
      set(hs(han_cont), 'XData', xdat{han_cont});
      set(hs(han_cont), 'YData', ydat{han_cont});

      % Find the label closest to the mouse click and delete it
      
      len_hh = length(hh);
      pos_hh = repmat(NaN, len_hh, 3);
      for ii = 1:len_hh
	pos_hh(ii, :) = get(hh(ii), 'Position');
      end
      vec_mag = (x - pos_hh(:, 1)).^2 + (y - pos_hh(:, 2)).^2;
      [junk, han_label] = min(vec_mag);
      delete(hh(han_label));
      hh(han_label) = [];
    case 5 % 'delete many labels using mouse'
      
      [x, y] = ginput(1);
      while(~isempty(x) & x > ax(1) & x < ax(2) & y > ax(3) & y < ax(4))
      	
	% Find the line segment closest to the mouse click and refresh it. This
	% will result in all of the other labels in that line segment having the
	% line drawn through them.
            
	vec_mag = repmat(NaN, len_hs, 1);
	for ii = 1:len_hs
	  vec_mag(ii) = min((x - xdat{ii}).^2 + (y - ydat{ii}).^2);
	end
	[junk, han_cont] = min(vec_mag);
      
	set(hs(han_cont), 'CData', cdat{han_cont});
	set(hs(han_cont), 'FaceVertexCData', fdat{han_cont});
	set(hs(han_cont), 'XData', xdat{han_cont});
	set(hs(han_cont), 'YData', ydat{han_cont});

	% Find the label closest to the mouse click and delete it
      
	len_hh = length(hh);
	pos_hh = repmat(NaN, len_hh, 3);
	for ii = 1:len_hh
	  pos_hh(ii, :) = get(hh(ii), 'Position');
	end
	vec_mag = (x - pos_hh(:, 1)).^2 + (y - pos_hh(:, 2)).^2;
	[junk, han_label] = min(vec_mag);
	delete(hh(han_label));
	hh(han_label) = [];
	
	[x, y] = ginput(1);
      end
    case 6 % 'delete all labels'
      for han_cont = 1:len_hs
	set(hs(han_cont), 'CData', cdat{han_cont});
	set(hs(han_cont), 'FaceVertexCData', fdat{han_cont});
	set(hs(han_cont), 'XData', xdat{han_cont});
	set(hs(han_cont), 'YData', ydat{han_cont});
      end
      delete(hh)
      hh = [];
    case 7 % 'specify # of significant figures in all labels'
      num_sig = input('# of significant figures?: ');
      for ii = 1:length(hh)
	set(hh(ii), 'String', num2str(get(hh(ii), 'UserData'), num_sig))
      end
    case 8 % 'make all negative contours dashed'
      
      userdata = zeros(len_hs, 1);
      for ii = 1:len_hs
	userdata(ii) = get(hs(ii), 'userdata');
      end
      set(hs(find(userdata<0)), 'linestyle', '--')
    case 9 % 'exit'
      break
  end
end

if nargout > 0
  han = hh;
end
