function [newstring] = EditString(titlestr, labelstr, string)
%function [newstring] = EditString(titlestr, labelstr, string)
% 
% Abstract:
%   Pop-up window with editable text control that allows user
%   to interactively change a string.
%
% 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 = EditString('Edit String', 'Text:', '');
%

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

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

  Wpos = [130 300 560 100];
  EditStrWin = figure('Position', [Wpos], ...
      'NumberTitle','off',...
      'Name', titlestr,...
      'NextPlot','new',...
      'Userdata', [1], ...
      'Tag','EditStrWin');
  
  LabelText = uicontrol('Parent', EditStrWin, ...
      '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', EditStrWin, ...
      'CallBack',...
      'set(findobj(0,''Tag'',''EditStrWin''), ''Userdata'', -1)', ... 
      'Style','edit',...
      'String', [string], ...
      'ForeGround', 'r', ...
      'BackGround', 'w', ...
      'Horiz', 'center', ...
      'Units', 'normal', ...
      'Position',[0.1 0.4 0.8 0.3], ...
      'Tag','EditText');
  
  % set userdata to original string
  set(EditText, 'Userdata', string);
  newstring = string;			% until user changes it!

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