Home > NoiseTools > private > listdlg3.m

# listdlg3

## PURPOSE

## SYNOPSIS

This is a script file.

## DESCRIPTION

## CROSS-REFERENCE INFORMATION

This function calls:

- getnicedialoglocation adjust the specified figure position to fig nicely over GCBF

This function is called by:

## SUBFUNCTIONS

- function [selection,value] = listdlg(varargin)
- function doKeypress(src, evd, listbox)
- function doOK(ok_btn, evd, listbox)
- function doCancel(cancel_btn, evd, listbox)
- function doSelectAll(selectall_btn, evd, listbox)
- function doListboxClick(listbox, evd, selectall_btn)

## SOURCE CODE

    0001 [a b c]=fileparts(evalc('which listdlg'));
    0002 addpath([a,'/private');
    0003
    0004 function [selection,value] = listdlg(varargin)
    0005 %LISTDLG  List selection dialog box.
    0006 %   [SELECTION,OK] = LISTDLG('ListString',S) creates a modal dialog box
    0007 %   which allows you to select a string or multiple strings from a list.
    0008 %   SELECTION is a vector of indices of the selected strings (length 1 in
    0009 %   the single selection mode).  This will be [] when OK is 0.  OK is 1 if
    0010 %   you push the OK button, or 0 if you push the Cancel button or close the
    0011 %   figure.
    0012 %
    0013 %   Double-clicking on an item or pressing <CR> when multiple items are
    0014 %   selected has the same effect as clicking the OK button.  Pressing <CR>
    0015 %   is the same as clicking the OK button. Pressing <ESC> is the same as
    0016 %   clicking the Cancel button.
    0017 %
    0018 %   Inputs are in parameter,value pairs:
    0019 %
    0020 %   Parameter       Description
    0021 %   'ListString'    cell array of strings for the list box.
    0022 %   'SelectionMode' string; can be 'single' or 'multiple'; defaults to
    0023 %                   'multiple'.
    0024 %   'ListSize'      [width height] of listbox in pixels; defaults
    0025 %                   to [160 300].
    0026 %   'InitialValue'  vector of indices of which items of the list box
    0027 %                   are initially selected; defaults to the first item.
    0028 %   'Name'          String for the figure's title; defaults to ''.
    0029 %   'PromptString'  string matrix or cell array of strings which appears
    0030 %                   as text above the list box; defaults to {}.
    0031 %   'OKString'      string for the OK button; defaults to 'OK'.
    0032 %   'CancelString'  string for the Cancel button; defaults to 'Cancel'.
    0033 %
    0034 %   A 'Select all' button is provided in the multiple selection case.
    0035 %
    0036 %   Example:
    0037 %     d = dir;
    0038 %     str = {d.name};
    0039 %     [s,v] = listdlg('PromptString','Select a file:',...
    0040 %                     'SelectionMode','single',...
    0041 %                     'ListString',str)
    0042  %
    0043 %  See also DIALOG, ERRORDLG, HELPDLG, INPUTDLG,
    0044 %    MSGBOX, QUESTDLG, WARNDLG.
    0045
    0046 %   Copyright 1984-2010 The MathWorks, Inc.
    0047 %   $Revision: 1.20.4.14 $  $Date: 2011/09/08 23:36:08 $
    0048
    0049 %   'uh'            uicontrol button height, in pixels; default = 22.
    0050 %   'fus'           frame/uicontrol spacing, in pixels; default = 8.
    0051 %   'ffs'           frame/figure spacing, in pixels; default = 8.
    0052
    0053 % simple test:
    0054 %
    0055 % d = dir; [s,v] = listdlg('PromptString','Select a file:','ListString',{d.name});
    0056 %
    0057
    0058 % Generate a warning in -nodisplay and -noFigureWindows mode.
    0059 warnfiguredialog('listdlg');
    0060
    0061 error(nargchk(1,inf,nargin))
    0062
    0063 figname = '';
    0064 smode = 2;   % (multiple)
    0065 promptstring = {};
    0066 liststring = [];
    0067 listsize = [160 300];
    0068 initialvalue = [];
    0069 okstring = getString(message('MATLAB:uistring:popupdialogs:OK'));
    0070 cancelstring = getString(message('MATLAB:uistring:popupdialogs:Cancel'));
    0071 fus = 8;
    0072 ffs = 8;
    0073 uh = 22;
    0074
    0075 if mod(length(varargin),2) ~= 0
    0076     % input args have not com in pairs, woe is me
    0077     error(message('MATLAB:listdlg:InvalidArgument'))
    0078 end
    0079 for i=1:2:length(varargin)
    0080     switch lower(varargin{i})
    0081      case 'name'
    0082       figname = varargin{i+1};
    0083      case 'promptstring'
    0084       promptstring = varargin{i+1};
    0085      case 'selectionmode'
    0086       switch lower(varargin{i+1})
    0087        case 'single'
    0088         smode = 1;
    0089        case 'multiple'
    0090         smode = 2;
    0091       end
    0092      case 'listsize'
    0093       listsize = varargin{i+1};
    0094      case 'liststring'
    0095       liststring = varargin{i+1};
    0096      case 'initialvalue'
    0097       initialvalue = varargin{i+1};
    0098      case 'uh'
    0099       uh = varargin{i+1};
    0100      case 'fus'
    0101       fus = varargin{i+1};
    0102      case 'ffs'
    0103       ffs = varargin{i+1};
    0104      case 'okstring'
    0105       okstring = varargin{i+1};
    0106      case 'cancelstring'
    0107       cancelstring = varargin{i+1};
    0108      otherwise
    0109       error(message('MATLAB:listdlg:UnknownParameter', varargin{ i }))
    0110     end
    0111 end
    0112
    0113 if ischar(promptstring)
    0114     promptstring = cellstr(promptstring);
    0115 end
    0116
    0117 if isempty(initialvalue)
    0118     initialvalue = 1;
    0119 end
    0120
    0121 if isempty(liststring)
    0122     error(message('MATLAB:listdlg:NeedParameter'))
    0123 end
    0124
    0125 ex = get(0,'DefaultUicontrolFontSize')*1.7;  % height extent per line of uicontrol text (approx)
    0126
    0127 fp = get(0,'DefaultFigurePosition');
    0128 w = 2*(fus+ffs)+listsize(1);
    0129 h = 2*ffs+6*fus+ex*length(promptstring)+listsize(2)+uh+(smode==2)*(fus+uh);
    0130 fp = [fp(1) fp(2)+fp(4)-h w h];  % keep upper left corner fixed
    0131
    0132 fig_props = { ...
    0133     'name'                   figname ...
    0134     'color'                  get(0,'DefaultUicontrolBackgroundColor') ...
    0135     'resize'                 'off' ...
    0136     'numbertitle'            'off' ...
    0137     'menubar'                'none' ...
    0138     'windowstyle'            'normal' ...
    0139     'visible'                'off' ...
    0140     'createfcn'              ''    ...
    0141     'position'               fp   ...
    0142     'closerequestfcn'        'delete(gcbf)' ...
    0143             };
    0144
    0145 liststring=cellstr(liststring);
    0146
    0147 fig = figure(fig_props{:});
    0148
    0149 if length(promptstring)>0
    0150     prompt_text = uicontrol('Style','text','String',promptstring,...
    0151         'HorizontalAlignment','left',...
    0152         'Position',[ffs+fus fp(4)-(ffs+fus+ex*length(promptstring)) ...
    0153         listsize(1) ex*length(promptstring)]); %#ok
    0154 end
    0155
    0156 btn_wid = (fp(3)-2*(ffs+fus)-fus)/2;
    0157
    0158 listbox = uicontrol('Style','listbox',...
    0159                     'Position',[ffs+fus ffs+uh+4*fus+(smode==2)*(fus+uh) listsize],...
    0160                     'String',liststring,...
    0161                     'BackgroundColor','w',...
    0162                     'Max',smode,...
    0163                     'Tag','listbox',...
    0164                     'Value',initialvalue, ...
    0165                     'Callback', {@doListboxClick});
    0166
    0167 ok_btn = uicontrol('Style','pushbutton',...
    0168                    'String',okstring,...
    0169                    'Position',[ffs+fus ffs+fus btn_wid uh],...
    0170                    'Tag','ok_btn',...
    0171                    'Callback',{@doOK,listbox});
    0172
    0173 cancel_btn = uicontrol('Style','pushbutton',...
    0174                        'String',cancelstring,...
    0175                        'Position',[ffs+2*fus+btn_wid ffs+fus btn_wid uh],...
    0176                        'Tag','cancel_btn',...
    0177                        'Callback',{@doCancel,listbox});
    0178
    0179 if smode == 2
    0180     selectall_btn = uicontrol('Style','pushbutton',...
    0181                               'String',getString(message('MATLAB:uistring:popupdialogs:SelectAll')),...
    0182                               'Position',[ffs+fus 4*fus+ffs+uh listsize(1) uh],...
    0183                               'Tag','selectall_btn',...
    0184                               'Callback',{@doSelectAll, listbox});
    0185
    0186     if length(initialvalue) == length(liststring)
    0187         set(selectall_btn,'Enable','off')
    0188     end
    0189     set(listbox,'Callback',{@doListboxClick, selectall_btn})
    0190 end
    0191
    0192 set([fig, ok_btn, cancel_btn, listbox], 'KeyPressFcn', {@doKeypress, listbox});
    0193
    0194 set(fig,'Position',getnicedialoglocation(fp, get(fig,'Units')));
    0195 % Make ok_btn the default button.
    0196 setdefaultbutton(fig, ok_btn);
    0197
    0198 % make sure we are on screen
    0199 movegui(fig)
    0200 set(fig, 'Visible','on'); drawnow;
    0201
    0202 try
    0203     % Give default focus to the listbox *after* the figure is made visible
    0204     uicontrol(listbox);
    0205     uiwait(fig);
    0206 catch
    0207     if ishghandle(fig)
    0208         delete(fig)
    0209     end
    0210 end
    0211
    0212 if isappdata(0,'ListDialogAppData__')
    0213     ad = getappdata(0,'ListDialogAppData__');
    0214     selection = ad.selection;
    0215     value = ad.value;
    0216     rmappdata(0,'ListDialogAppData__')
    0217 else
    0218     % figure was deleted
    0219     selection = [];
    0220     value = 0;
    0221 end
    0222
    0223 %% figure, OK and Cancel KeyPressFcn
    0224 function doKeypress(src, evd, listbox) %#ok
    0225 switch evd.Key
    0226  case 'escape'
    0227   doCancel([],[],listbox);
    0228 end
    0229
    0230 %% OK callback
    0231 function doOK(ok_btn, evd, listbox) %#ok
    0232 if (~isappdata(0, 'ListDialogAppData__'))
    0233     ad.value = 1;
    0234     ad.selection = get(listbox,'Value');
    0235     setappdata(0,'ListDialogAppData__',ad);
    0236     delete(gcbf);
    0237 end
    0238
    0239 %% Cancel callback
    0240 function doCancel(cancel_btn, evd, listbox) %#ok
    0241 ad.value = 0;
    0242 ad.selection = [];
    0243 setappdata(0,'ListDialogAppData__',ad)
    0244 delete(gcbf);
    0245
    0246 %% SelectAll callback
    0247 function doSelectAll(selectall_btn, evd, listbox) %#ok
    0248 set(selectall_btn,'Enable','off')
    0249 set(listbox,'Value',1:length(get(listbox,'String')));
    0250
    0251 %% Listbox callback
    0252 function doListboxClick(listbox, evd, selectall_btn) %#ok
    0253 % if this is a doubleclick, doOK
    0254 if strcmp(get(gcbf,'SelectionType'),'open')
    0255     doOK([],[],listbox);
    0256 elseif nargin == 3
    0257     if length(get(listbox,'String'))==length(get(listbox,'Value'))
    0258         set(selectall_btn,'Enable','off')
    0259     else
    0260         set(selectall_btn,'Enable','on')
    0261     end
    0262 end