0001 [a b c]=fileparts(evalc('which listdlg'));
0002 addpath([a,'/private');
0003
0004 function [selection,value] = listdlg(varargin)
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059 warnfiguredialog('listdlg');
0060
0061 error(nargchk(1,inf,nargin))
0062
0063 figname = '';
0064 smode = 2;
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
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;
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];
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)]);
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
0196 setdefaultbutton(fig, ok_btn);
0197
0198
0199 movegui(fig)
0200 set(fig, 'Visible','on'); drawnow;
0201
0202 try
0203
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
0219 selection = [];
0220 value = 0;
0221 end
0222
0223
0224 function doKeypress(src, evd, listbox)
0225 switch evd.Key
0226 case 'escape'
0227 doCancel([],[],listbox);
0228 end
0229
0230
0231 function doOK(ok_btn, evd, listbox)
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
0240 function doCancel(cancel_btn, evd, listbox)
0241 ad.value = 0;
0242 ad.selection = [];
0243 setappdata(0,'ListDialogAppData__',ad)
0244 delete(gcbf);
0245
0246
0247 function doSelectAll(selectall_btn, evd, listbox)
0248 set(selectall_btn,'Enable','off')
0249 set(listbox,'Value',1:length(get(listbox,'String')));
0250
0251
0252 function doListboxClick(listbox, evd, selectall_btn)
0253
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