function Answer = getdatadlg(Prompt, Title, NumLines, DefAns)
%GETDATADLG Input dialog box.
%  Answer = getdatadlg(Prompt) creates a modal dialog box that returns
%  user input for multiple prompts in the cell array Answer.  Prompt
%  is a cell array containing the Prompt strings.
%
%  Answer = getdatadlg(Prompt,Title) specifies the Title for the dialog.
%  Use: Title = sprintf('xxxx \n xxx'); to accomodate long text strings.
%
%  Answer = getdatadlg(Prompt,Title,LineNo) specifies the number of lines
%  for each answer in LineNo.  LineNo may be a constant value or a 
%  column vector having one element per Prompt that specifies how many
%  lines per input.  LineNo may also be a matrix where the first
%  column specifies how many rows for the input field and the second
%  column specifies how many columns wide the input field should be.
%
%  Answer = getdatadlg(Prompt,Title,LineNo,DefAns) specifies the default
%  answer to display for each Prompt.  DefAns must contain the same
%  number of elements as Prompt and must be a cell array.
%
%  Example:
%  prompt={'Enter the matrix size for x^2:','Enter the colormap name:'};
%  def={'20','hsv'};
%  dlgTitle='Input for Peaks function';
%  lineNo=1;
%  answer=getdatadlg(prompt,dlgTitle,lineNo,def);
%
%  <Tab> navigates between editboxes. <Return> in the last (or only)
%  editbox = OK. Any change in the last editbox + <Tab> also = OK.
%  OK button accepts click but not <Return>.

%  Modeled after inputdlg.m  12/'00
%  Custom version needed to preserve original behavior of 5.3.1 (R11.1) 
%  which was changed in version R(12.x).
%  "Sub-classing" also needed to fix MathWork's bug of unstable 'position',
%  and to provide <Return> key functionality.



% Check input
%-------------------------------------------------------------------------
if nargin == 1 & nargout == 0,
   if strcmp(Prompt,'getdatadlgResizeCB'),
      LocalResizeFcn(gcbf)
      return
   end
end

if nargout~=1,error('Wrong number of output arguments for getdatadlg');end
if nargin<1,error('Too few arguments for getdatadlg');end

if nargin==1,
   Title=' ';
end

if nargin<=2, NumLines=1;end

if ~iscell(Prompt),
   Prompt={Prompt};
end

NumQuest=prod(size(Prompt));    

if nargin<=3, 
   DefAns=cell(NumQuest,1);
   for lp=1:NumQuest, DefAns{lp}=''; end
end

WindowStyle='normal';
Interpreter='none';

if nargin>4,error('Too many input arguments');end

% Backwards Compatibility
if isstr(NumLines),
   warning(['Please see the getdatadlg help for correct input syntax.' 10 ...
         '         OKCallback no longer supported.' ]);
   NumLines=1;
end

[rw,cl]=size(NumLines);
OneVect = ones(NumQuest,1);
if (rw == 1 & cl == 2)
   NumLines=NumLines(OneVect,:);
elseif (rw == 1 & cl == 1)
   NumLines=NumLines(OneVect);
elseif (rw == 1 & cl == NumQuest)
   NumLines = NumLines'
elseif rw ~= NumQuest | cl > 2,
   error('NumLines size is incorrect.')
end

if ~iscell(DefAns),
   error('Default Answer must be a cell array in getdatadlg.');  
end

% Create input figure
%-------------------------------------------------------------------------
Temp=get(0,'Units');
set(0,'Units','points');
ScreenSize=get(0,'ScreenSize');

FigWidth=300;FigHeight=100;
FigPos(3:4)=[FigWidth FigHeight];
FigColor=[.92 .92 .97];
InputFig=dialog(                   ...
   'Visible'         ,'off'      , ...
   'Name'            ,Title      , ...
   'Pointer'         ,'arrow'    , ...
   'Units'           ,'points'   , ...
   'UserData'        ,''         , ...
   'Tag'             ,Title      , ...
   'HandleVisibility','on'       , ...
   'Color'           ,FigColor   , ...
   'NextPlot'        ,'add'      , ...
   'WindowStyle'     ,WindowStyle  ...
);


% Set positions
%-------------------------------------------------------------------------
DefOffset=5;
SmallOffset=2;

DefBtnWidth=50;
BtnHeight=20;
BtnYOffset=DefOffset;
BtnFontSize=get(0,'FactoryUIControlFontSize');
BtnWidth=DefBtnWidth;
TxtBackClr=[.87 .8 .8];
TxtForeClr='k';

TextInfo.Units              ='points'   ;   
TextInfo.FontSize           =BtnFontSize;
TextInfo.HorizontalAlignment='left'     ;
TextInfo.HandleVisibility   ='callback' ;

StInfo=TextInfo;
StInfo.Style              ='text'     ;
StInfo.BackgroundColor    =TxtBackClr ;
StInfo.ForegroundColor    =TxtForeClr ;

TextInfo.VerticalAlignment='bottom';

EdInfo=StInfo;
EdInfo.Style='edit';
EdInfo.BackgroundColor='w';

BtnInfo=StInfo;
BtnInfo.Style='pushbutton';
BtnInfo.HorizontalAlignment='center';

% Determine # of lines for all Prompts
ExtControl=uicontrol(StInfo, ...
   'String'   ,''          , ...    
   'Position' ,[DefOffset    ...              
                  DefOffset  ...
                  0.96*(FigWidth-2*DefOffset) ...
                  BtnHeight  ...
              ]            , ...
   'Visible'  ,'off'         ...
   );

WrapQuest=cell(NumQuest,1);
QuestPos=zeros(NumQuest,4);

for ExtLp=1:NumQuest,
   if size(NumLines,2)==2
      [WrapQuest{ExtLp},QuestPos(ExtLp,1:4)]= ...
         textwrap(ExtControl,Prompt(ExtLp),NumLines(ExtLp,2));
   else,
      [WrapQuest{ExtLp},QuestPos(ExtLp,1:4)]= ...
         textwrap(ExtControl,Prompt(ExtLp),80);
   end
end % for ExtLp

delete(ExtControl);
QuestHeight=QuestPos(:,4);

TxtHeight=QuestHeight(1)/size(WrapQuest{1,1},1);
EditHeight=TxtHeight*NumLines(:,1);
EditHeight(NumLines(:,1)==1)=EditHeight(NumLines(:,1)==1)+4;

FigHeight=(NumQuest+2)*DefOffset    + ...
   BtnHeight+sum(EditHeight) + ...
   sum(QuestHeight);

TxtXOffset=DefOffset;
TxtWidth=FigWidth-2*DefOffset;
TxtForeClr='k';
TxtBackClr=get(InputFig,'Color');

QuestYOffset=zeros(NumQuest,1);
EditYOffset=zeros(NumQuest,1);
QuestYOffset(1)=FigHeight-DefOffset-QuestHeight(1);
EditYOffset(1)=QuestYOffset(1)-EditHeight(1);% -SmallOffset;

for YOffLp=2:NumQuest,
   QuestYOffset(YOffLp)=EditYOffset(YOffLp-1)-QuestHeight(YOffLp)-DefOffset;
   EditYOffset(YOffLp)=QuestYOffset(YOffLp)-EditHeight(YOffLp); %-SmallOffset;
end % for YOffLp

QuestHandle=[];
EditHandle=[];
FigWidth =1;

AxesHandle=axes('Parent',InputFig,'Position',[0 0 1 1],'Visible','off');

for lp=1:NumQuest,
   QuestTag=['Prompt' num2str(lp)];
   EditTag=['Edit', num2str(lp)];
   if lp == NumQuest, EditTag = 'keyref'; end;
   if ~ischar(DefAns{lp}),
      delete(InputFig);
      error('Default answers must be strings in getdatadlg.');
   end
   QuestHandle(lp)=text('Parent',AxesHandle, ...
      TextInfo     , ...
      'Position'   ,[ TxtXOffset QuestYOffset(lp)], ...
      'FontSize'   , 13                           , ...
      'String'     ,WrapQuest{lp}                 , ...
      'Interpreter',Interpreter                   , ...
      'Tag'        ,QuestTag                        ...
   );
  
   Gostring = ''; % Gostring = 'set(gcbo,''Tag'',''Go'')';
   EditHandle(lp)=uicontrol(InputFig   ,EdInfo    , ...
      'Max'       ,NumLines(lp,1)                 , ...
      'Position'  ,[ TxtXOffset EditYOffset(lp)     ...
                         TxtWidth   EditHeight(lp)  ...
                   ]                              , ...
      'String'    ,DefAns{lp}                     , ...
      'Callback'  ,Gostring                       , ...
      'Tag'       ,EditTag                         ...
   );
   if size(NumLines,2) == 2,
      set(EditHandle(lp),'String',char(ones(1,NumLines(lp,2))*'x'));
      Extent = get(EditHandle(lp),'Extent');
      NewPos = [TxtXOffset EditYOffset(lp)  Extent(3) EditHeight(lp) ];
      NewPos1= [TxtXOffset QuestYOffset(lp)];
      set(EditHandle(lp),'Position',NewPos,'String',DefAns{lp})
      set(QuestHandle(lp),'Position',NewPos1)
      
      FigWidth=max(FigWidth,Extent(3)+2*DefOffset);
   else
      FigWidth=max(175,TxtWidth+2*DefOffset);
   end
   
end % for lp

FigPos=get(InputFig,'Position');

%original site of 4 'Units' commands (--> 3 fwd; 1 @ end)

FigWidth=max(FigWidth,2*(BtnWidth+DefOffset)+DefOffset);
FigPos(1)=(ScreenSize(3)-FigWidth)/2;
FigPos(2)=(ScreenSize(4)-FigHeight)/2;
FigPos(3)=FigWidth;
FigPos(4)=FigHeight;

set(InputFig,'Position',FigPos);

CBString1 = 'disp(''ye gads'');set(gcbo,''Tag'',''Cancel'');set(findobj(''Tag'',''keyref''),''Tag'',''Go'')';
%CBString1 = 'disp(''ye gads'');set(findobj(''Tag'',''NoCancel''),''Tag'',''Cancel'');set(findobj(''Tag'',''keyref''),''Tag'',''Go'')';
%CBString1 = 'disp(''ye gads'');set(CancelHandle,''Tag'',''Cancel'');set(findobj(''Tag'',''keyref''),''Tag'',''Go'')';
CancelHandle=uicontrol(InputFig   ,              ...
   BtnInfo     , ...
   'Position'  ,[ DefOffset DefOffset ...
      BtnWidth  BtnHeight  ...
                 ]           , ...
   'String'    ,'Cancel'     , ...
   'Callback'  ,CBString1    , ...
   'Tag'       , 'NoCancel'   ...   
   );

CBString2='set(findobj(''Tag'',''Cancel''),''Tag'',''NoCancel'');set(findobj(''Tag'',''keyref''),''Tag'',''Go'')';
OKHandle=uicontrol(InputFig    ,              ...
   BtnInfo      , ...
   'Position'   ,[ FigWidth-BtnWidth-DefOffset DefOffset ...
      BtnWidth                    BtnHeight ...
                 ]           , ...
   'String'     ,'OK'        , ...
   'Callback'   ,CBString2   , ...
   'Tag'        ,'OK'          ...
   );

Data.OKHandle = OKHandle;
Data.CancelHandle = CancelHandle;
Data.EditHandles = EditHandle;
Data.QuestHandles = QuestHandle;
Data.LineInfo = NumLines;
Data.ButtonWidth = BtnWidth;
Data.ButtonHeight = BtnHeight;
Data.EditHeight = TxtHeight+4;
Data.Offset = DefOffset;
set(InputFig ,'Visible','on','UserData',Data);
% This drawnow is a hack to work around a bug (MathWorks)
drawnow
set(findall(InputFig),'Units','normalized','HandleVisibility','callback');
set(InputFig,'Units','points')

waitfor(EditHandle(NumQuest),'Tag','Go');

TempHide=get(0,'ShowHiddenHandles');
set(0,'ShowHiddenHandles','on');

if any(get(0,'Children')==InputFig),
   if strcmp(get(CancelHandle,'Tag'),'NoCancel')
      Answer=cell(NumQuest,1);
      for lp=1:NumQuest,
         Answer(lp)=get(EditHandle(lp),{'String'});
      end % for
      delete(InputFig);
   else
      Answer={};
      delete(InputFig);
   end
else,
   Answer={};
end % if any

set(0,'ShowHiddenHandles',TempHide);
drawnow;
set(0,'Units',Temp);  % bug fix

