function [newvalue] = EditValue(titlestr, labelstr, value)
%function [newvalue] = EditValue(titlestr, labelstr, value)
% 
% Abstract:
%   Pop-up window with editable text control that allows user
%   to interactively change a floating point value.
%
% Usage:
%   Intended to be used from another program that requires user
%   input or user requests change.  The window stays open and 
%   consequently the function is in a wait state until user
%   completes requested change.
%
%   >> option = EditValue('Edit Value', 'Marker Size:', 1);
%

% History:
%   o 11 August 1995 create function, window, by Sara Haines
%

if nargin < 3
  disp('EditValue: error!  Not enough input arguments');
  return;
end

  
  Wpos = [130 300 125 100];
  EditWin = figure('Position', [Wpos], ...
      'NumberTitle','off',...
      'Name', titlestr,...
      'NextPlot','new',...
      'Userdata', [1], ...
      'Tag','EditWin');
  
  LabelText = uicontrol('Parent', EditWin, ...
      'CallBack','', ...          
      'Style','text',...
      'String', labelstr, ...
      'ForeGround', 'w', ...
      'BackGround', 'k', ...
      'Horiz', 'center', ...
      'Units', 'normal', ...
      'Position',[0.1 0.7 0.8 0.3], ...
      'Tag','LabelText');

  EditText = uicontrol('Parent', EditWin, ...
      'CallBack','set(findobj(0,''Tag'',''EditWin''), ''Userdata'', -1)', ... 
      'Style','edit',...
      'String', num2str(value), ...
      'ForeGround', 'r', ...
      'BackGround', 'w', ...
      'Horiz', 'center', ...
      'Units', 'normal', ...
      'Position',[0.2 0.4 0.6 0.3], ...
      'Tag','EditText');
  
  % set userdata to original value
  set(EditText, 'Userdata', value);
  newvalue = value;			% until user changes it!

  CancelButton = uicontrol('Parent',EditWin,...   
      'Callback','set(findobj(0,''Tag'',''EditWin''), ''Userdata'', 0)',...
      'BackgroundColor',[1;0;0],...
      'Style','pushbutton',...
      'String','Cancel',...
      'Units', 'normal', ...
      'Position',[0.3 0.1 0.4 0.2],...
      'Tag','CancelButton');
  
  % purposefully suspend other action until user finishes edit change
  % or presses the cancel button
  figure(EditWin);
  while get(EditWin, 'Userdata')==1
    figure(EditWin);
    pause(1);
  end
  
  newvalue = str2num(get(EditText, 'String'));
  close(EditWin);
