source: roi_inputdlg.m @ 2

Last change on this file since 2 was 2, checked in by tjniskan, 12 years ago
  • Imported first revision
File size: 13.5 KB
RevLine 
[2]1function Answer=roi_inputdlg(Prompt, Title, NumLines, DefAns, Resize)
2%INPUTDLG Input dialog box.
3%  ANSWER = INPUTDLG(PROMPT) creates a modal dialog box that returns user
4%  input for multiple prompts in the cell array ANSWER. PROMPT is a cell
5%  array containing the PROMPT strings.
6%
7%  INPUTDLG uses UIWAIT to suspend execution until the user responds.
8%
9%  ANSWER = INPUTDLG(PROMPT,NAME) specifies the title for the dialog.
10%
11%  ANSWER = INPUTDLG(PROMPT,NAME,NUMLINES) specifies the number of lines for
12%  each answer in NUMLINES. NUMLINES may be a constant value or a column
13%  vector having one element per PROMPT that specifies how many lines per
14%  input field. NUMLINES may also be a matrix where the first column
15%  specifies how many rows for the input field and the second column
16%  specifies how many columns wide the input field should be.
17%
18%  ANSWER = INPUTDLG(PROMPT,NAME,NUMLINES,DEFAULTANSWER) specifies the
19%  default answer to display for each PROMPT. DEFAULTANSWER must contain
20%  the same number of elements as PROMPT and must be a cell array of
21%  strings.
22%
23%  ANSWER = INPUTDLG(PROMPT,NAME,NUMLINES,DEFAULTANSWER,OPTIONS) specifies
24%  additional options. If OPTIONS is the string 'on', the dialog is made
25%  resizable. If OPTIONS is a structure, the fields Resize, WindowStyle, and
26%  Interpreter are recognized. Resize can be either 'on' or
27%  'off'. WindowStyle can be either 'normal' or 'modal'. Interpreter can be
28%  either 'none' or 'tex'. If Interpreter is 'tex', the prompt strings are
29%  rendered using LaTeX.
30%
31%  Examples:
32%
33%  prompt={'Enter the matrix size for x^2:','Enter the colormap name:'};
34%  name='Input for Peaks function';
35%  numlines=1;
36%  defaultanswer={'20','hsv'};
37%
38%  answer=inputdlg(prompt,name,numlines,defaultanswer);
39%
40%  options.Resize='on';
41%  options.WindowStyle='normal';
42%  options.Interpreter='tex';
43%
44%  answer=inputdlg(prompt,name,numlines,defaultanswer,options);
45%
46%  See also TEXTWRAP, ERRORDLG, HELPDLG, QUESTDLG, WARNDLG, UIWAIT.
47
48%  Copyright 1994-2005 The MathWorks, Inc.
49%  $Revision: 1.58.4.9 $
50
51%%%%%%%%%%%%%%%%%%%%
52%%% Nargin Check %%%
53%%%%%%%%%%%%%%%%%%%%
54error(nargchk(0,5,nargin));
55error(nargoutchk(0,1,nargout));
56
57%%%%%%%%%%%%%%%%%%%%%%%%%
58%%% Handle Input Args %%%
59%%%%%%%%%%%%%%%%%%%%%%%%%
60if nargin<1
61    Prompt='Input:';
62end
63if ~iscell(Prompt)
64    Prompt={Prompt};
65end
66NumQuest=numel(Prompt);
67
68
69if nargin<2,
70    Title=' ';
71end
72
73if nargin<3
74    NumLines=1;
75end
76
77if nargin<4
78    DefAns=cell(NumQuest,1);
79    for lp=1:NumQuest
80        DefAns{lp}='';
81    end
82end
83
84if nargin<5
85    Resize = 'off';
86end
87WindowStyle='modal';
88Interpreter='none';
89
90Options = struct([]); %#ok
91if nargin==5 && isstruct(Resize)
92    Options = Resize;
93    Resize  = 'off';
94    if isfield(Options,'Resize'),      Resize=Options.Resize;           end
95    if isfield(Options,'WindowStyle'), WindowStyle=Options.WindowStyle; end
96    if isfield(Options,'Interpreter'), Interpreter=Options.Interpreter; end
97end
98
99[rw,cl]=size(NumLines);
100OneVect = ones(NumQuest,1);
101if (rw == 1 & cl == 2) %#ok Handle []
102    NumLines=NumLines(OneVect,:);
103elseif (rw == 1 & cl == 1) %#ok
104    NumLines=NumLines(OneVect);
105elseif (rw == 1 & cl == NumQuest) %#ok
106    NumLines = NumLines';
107elseif (rw ~= NumQuest | cl > 2) %#ok
108    error('NumLines size is incorrect.')
109end
110
111if ~iscell(DefAns),
112    error('Default Answer must be a cell array of strings.'); 
113end
114
115%%%%%%%%%%%%%%%%%%%%%%%
116%%% Create InputFig %%%
117%%%%%%%%%%%%%%%%%%%%%%%
118FigWidth=175;
119FigHeight=100;
120FigPos(3:4)=[FigWidth FigHeight];
121FigColor=get(0,'DefaultUicontrolBackgroundcolor');
122
123InputFig=dialog(                     ...
124    'Visible'          ,'off'      , ...
125    'KeyPressFcn'      ,@doFigureKeyPress, ...
126    'Name'             ,Title      , ...
127    'Pointer'          ,'arrow'    , ...
128    'Units'            ,'pixels'   , ...
129    'UserData'         ,'Cancel'   , ...
130    'Tag'              ,Title      , ...
131    'HandleVisibility' ,'callback' , ...
132    'Color'            ,FigColor   , ...
133    'NextPlot'         ,'add'      , ...
134    'WindowStyle'      ,WindowStyle, ...
135    'DoubleBuffer'     ,'on'       , ...
136    'Resize'           ,Resize       ...
137    );
138
139
140%%%%%%%%%%%%%%%%%%%%%
141%%% Set Positions %%%
142%%%%%%%%%%%%%%%%%%%%%
143DefOffset    = 5;
144DefBtnWidth  = 53;
145DefBtnHeight = 23;
146
147TextInfo.Units              = 'pixels'   ;   
148TextInfo.FontSize           = get(0,'FactoryUIControlFontSize');
149TextInfo.FontWeight         = get(InputFig,'DefaultTextFontWeight');
150TextInfo.HorizontalAlignment= 'left'     ;
151TextInfo.HandleVisibility   = 'callback' ;
152
153StInfo=TextInfo;
154StInfo.Style              = 'text'  ;
155StInfo.BackgroundColor    = FigColor;
156
157
158EdInfo=StInfo;
159EdInfo.FontWeight      = get(InputFig,'DefaultUicontrolFontWeight');
160EdInfo.Style           = 'edit';
161EdInfo.BackgroundColor = 'white';
162
163BtnInfo=StInfo;
164BtnInfo.FontWeight          = get(InputFig,'DefaultUicontrolFontWeight');
165BtnInfo.Style               = 'pushbutton';
166BtnInfo.HorizontalAlignment = 'center';
167
168% Add VerticalAlignment here as it is not applicable to the above.
169TextInfo.VerticalAlignment  = 'bottom';
170TextInfo.Color              = get(0,'FactoryUIControlForegroundColor');
171
172
173% adjust button height and width
174btnMargin=1.4;
175ExtControl=uicontrol(InputFig   ,BtnInfo     , ...
176                     'String'   ,'OK'        , ...
177                     'Visible'  ,'off'         ...
178                     );
179
180% BtnYOffset  = DefOffset;
181BtnExtent = get(ExtControl,'Extent');
182BtnWidth  = max(DefBtnWidth,BtnExtent(3)+8);
183BtnHeight = max(DefBtnHeight,BtnExtent(4)*btnMargin);
184delete(ExtControl);
185
186% Determine # of lines for all Prompts
187TxtWidth=FigWidth-2*DefOffset;
188ExtControl=uicontrol(InputFig   ,StInfo     , ...
189                     'String'   ,''         , ...
190                     'Position' ,[ DefOffset DefOffset 0.96*TxtWidth BtnHeight ] , ...
191                     'Visible'  ,'off'        ...
192                     );
193
194WrapQuest=cell(NumQuest,1);
195QuestPos=zeros(NumQuest,4);
196
197for ExtLp=1:NumQuest
198    if size(NumLines,2)==2
199        [WrapQuest{ExtLp},QuestPos(ExtLp,1:4)]= ...
200            textwrap(ExtControl,Prompt(ExtLp),NumLines(ExtLp,2));
201    else
202        [WrapQuest{ExtLp},QuestPos(ExtLp,1:4)]= ...
203            textwrap(ExtControl,Prompt(ExtLp),80);
204    end
205end % for ExtLp
206
207delete(ExtControl);
208QuestWidth =QuestPos(:,3);
209QuestHeight=QuestPos(:,4);
210
211TxtHeight=QuestHeight(1)/size(WrapQuest{1,1},1);
212EditHeight=TxtHeight*NumLines(:,1);
213EditHeight(NumLines(:,1)==1)=EditHeight(NumLines(:,1)==1)+4;
214
215FigHeight=(NumQuest+2)*DefOffset    + ...
216          BtnHeight+sum(EditHeight) + ...
217          sum(QuestHeight);
218
219TxtXOffset=DefOffset;
220
221QuestYOffset=zeros(NumQuest,1);
222EditYOffset=zeros(NumQuest,1);
223QuestYOffset(1)=FigHeight-DefOffset-QuestHeight(1);
224EditYOffset(1)=QuestYOffset(1)-EditHeight(1);
225
226for YOffLp=2:NumQuest,
227    QuestYOffset(YOffLp)=EditYOffset(YOffLp-1)-QuestHeight(YOffLp)-DefOffset;
228    EditYOffset(YOffLp)=QuestYOffset(YOffLp)-EditHeight(YOffLp);
229end % for YOffLp
230
231QuestHandle=[]; %#ok
232EditHandle=[];
233
234AxesHandle=axes('Parent',InputFig,'Position',[0 0 1 1],'Visible','off');
235
236inputWidthSpecified = false;
237
238for lp=1:NumQuest,
239    if ~ischar(DefAns{lp}),
240        delete(InputFig);
241        error('Default Answer must be a cell array of strings.'); 
242    end
243
244    EditHandle(lp)=uicontrol(InputFig    , ...
245                             EdInfo      , ...
246                             'Max'        ,NumLines(lp,1)       , ...
247                             'Position'   ,[ TxtXOffset EditYOffset(lp) TxtWidth EditHeight(lp) ], ...
248                             'String'     ,DefAns{lp}           , ...
249                             'Tag'        ,'Edit'               ,  ...
250                             'KeyPressFcn',@doControlKeyPress   , ...
251                             'Userdata','Edit');
252
253    QuestHandle(lp)=text('Parent'     ,AxesHandle, ...
254                         TextInfo     , ...
255                         'Position'   ,[ TxtXOffset QuestYOffset(lp)], ...
256                         'String'     ,WrapQuest{lp}                 , ...
257                         'Interpreter',Interpreter                   , ...
258                         'Tag'        ,'Quest'                         ...
259                         );
260
261    MinWidth = max(QuestWidth(:));
262    if (size(NumLines,2) == 2)
263        % input field width has been specified.
264        inputWidthSpecified = true;
265        EditWidth = setcolumnwidth(EditHandle(lp), NumLines(lp,1), NumLines(lp,2));
266        MinWidth = max(MinWidth, EditWidth);
267    end
268    FigWidth=max(FigWidth, MinWidth+2*DefOffset);
269
270end % for lp
271
272% fig width may have changed, update the edit fields if they dont have user specified widths.
273if ~inputWidthSpecified
274    TxtWidth=FigWidth-2*DefOffset;
275    for lp=1:NumQuest
276        set(EditHandle(lp), 'Position', [TxtXOffset EditYOffset(lp) TxtWidth EditHeight(lp)]);
277    end
278end
279
280FigPos=get(InputFig,'Position');
281
282FigWidth=max(FigWidth,2*(BtnWidth+DefOffset)+DefOffset);
283FigPos(1)=0;
284FigPos(2)=0;
285FigPos(3)=FigWidth;
286FigPos(4)=FigHeight;
287
288set(InputFig,'Position',getnicedialoglocation(FigPos,get(InputFig,'Units')));
289
290OKHandle=uicontrol(InputFig     ,              ...
291                   BtnInfo      , ...
292                   'Position'   ,[ FigWidth-2*BtnWidth-2*DefOffset DefOffset BtnWidth BtnHeight ] , ...
293                   'KeyPressFcn',@doControlKeyPress , ...
294                   'String'     ,'OK'        , ...
295                   'Callback'   ,@doCallback , ...
296                   'Tag'        ,'OK'        , ...
297                   'UserData'   ,'OK'          ...
298                   );
299
300%setdefaultbutton(InputFig, OKHandle);
301
302CancelHandle=uicontrol(InputFig     ,              ...
303                       BtnInfo      , ...
304                       'Position'   ,[ FigWidth-BtnWidth-DefOffset DefOffset BtnWidth BtnHeight ]           , ...
305                       'KeyPressFcn',@doControlKeyPress            , ...
306                       'String'     ,'Cancel'    , ...
307                       'Callback'   ,@doCallback , ...
308                       'Tag'        ,'Cancel'    , ...
309                       'UserData'   ,'Cancel'      ...
310                       ); %#ok
311
312handles = guihandles(InputFig);
313handles.MinFigWidth = FigWidth;
314handles.FigHeight   = FigHeight;
315handles.TextMargin  = 2*DefOffset;
316guidata(InputFig,handles);
317set(InputFig,'ResizeFcn', {@doResize, inputWidthSpecified});
318
319% make sure we are on screen
320movegui(InputFig)
321
322% if there is a figure out there and it's modal, we need to be modal too
323if ~isempty(gcbf) && strcmp(get(gcbf,'WindowStyle'),'modal')
324    set(InputFig,'WindowStyle','modal');
325end
326
327set(InputFig,'Visible','on');
328drawnow;
329
330if ~isempty(EditHandle)
331    uicontrol(EditHandle(1));
332end
333
334uiwait(InputFig);
335
336% For some mysterious reason, without the drawnow command here the string
337% from the EditHandle is empty... go figure...
338drawnow
339
340if ishandle(InputFig)
341    Answer={};
342    if strcmp(get(InputFig,'UserData'),'OK'),
343        Answer=cell(NumQuest,1);
344        for lp=1:NumQuest,
345            Answer(lp)=get(EditHandle(lp),{'String'});
346        end
347    end
348    delete(InputFig);
349else
350    Answer={};
351end
352
353function doFigureKeyPress(obj, evd) %#ok
354switch(evd.Key)
355 case {'return','space'}
356  set(gcbf,'UserData','OK');
357  uiresume(gcbf);
358 case {'escape'}
359  delete(gcbf);
360end
361
362function doControlKeyPress(obj, evd) %#ok
363switch(evd.Key)
364 case {'return'}
365  if ~isempty(get(obj,'userdata')) && ...
366        strcmpi(get(obj,'userdata'),'Edit')
367    set(gcbf,'UserData','OK');
368    uiresume(gcbf);
369  elseif ~strcmp(get(obj,'UserData'),'Cancel')
370      set(gcbf,'UserData','OK');
371      uiresume(gcbf);
372  else
373      delete(gcbf)
374  end
375 case 'escape'
376  delete(gcbf)
377end
378
379function doCallback(obj, evd) %#ok
380if ~strcmp(get(obj,'UserData'),'Cancel')
381    set(gcbf,'UserData','OK');
382    uiresume(gcbf);
383else
384    delete(gcbf)
385end
386
387function doResize(FigHandle, evd, multicolumn) %#ok
388% TBD: Check difference in behavior w/ R13. May need to implement
389% additional resize behavior/clean up.
390
391Data=guidata(FigHandle);
392
393resetPos = false;
394
395FigPos = get(FigHandle,'Position');
396FigWidth = FigPos(3);
397FigHeight = FigPos(4);
398
399if FigWidth < Data.MinFigWidth
400    FigWidth  = Data.MinFigWidth;
401    FigPos(3) = Data.MinFigWidth;
402    resetPos = true;
403end
404
405% make sure edit fields use all available space if
406% number of columns is not specified in dialog creation.
407if ~multicolumn
408    for lp = 1:length(Data.Edit)
409        EditPos = get(Data.Edit(lp),'Position');
410        EditPos(3) = FigWidth - Data.TextMargin;
411        set(Data.Edit(lp),'Position',EditPos);
412    end
413end
414
415if FigHeight ~= Data.FigHeight
416    FigPos(4) = Data.FigHeight;
417    resetPos = true;
418end
419
420if resetPos
421    set(FigHandle,'Position',FigPos); 
422end
423
424
425% set pixel width given the number of columns
426function EditWidth = setcolumnwidth(object, rows, cols)
427% Save current Units and String.
428old_units = get(object, 'Units');
429old_string = get(object, 'String');
430old_position = get(object, 'Position');
431
432set(object, 'Units', 'pixels')
433set(object, 'String', char(ones(1,cols)*'x'));
434
435new_extent = get(object,'Extent');
436if (rows > 1)
437    % For multiple rows, allow space for the scrollbar
438    new_extent = new_extent + 19; % Width of the scrollbar
439end
440new_position = old_position;
441new_position(3) = new_extent(3) + 1;
442set(object, 'Position', new_position);
443
444% reset string and units
445set(object, 'String', old_string, 'Units', old_units);
446
447EditWidth = new_extent(3);
Note: See TracBrowser for help on using the repository browser.

Powered by Trac 1.0.9.Copyright © Juha-Pekka Niskanen 2008