www.gusucode.com > mbcguitools 工具箱 matlab 源码程序 > mbcguitools/mv_listdlg.m
function [selection,value] = mv_listdlg(varargin) %LISTDLG List selection dialog box. % [SELECTION,OK] = LISTDLG('ListString',S) creates a modal dialog box % which allows you to select a string or multiple strings from a list. % SELECTION is a vector of indices of the selected strings (length 1 % in the single selection mode). Can be [] on the Mac, and will be [] % when OK is 0. % OK is 1 if you push the OK button, or 0 if you push the Cancel % button or close the figure. % Double-clicking on an item or pressing <CR> when multiple items are % selected has the same effect as clicking the OK button. % % Inputs are in parameter,value pairs: % % Parameter Description % 'ListString' cell array of strings for the list box. % 'SelectionMode' string; can be 'single' or 'multiple'; defaults to % 'multiple'. % 'ListSize' [width height] of listbox in pixels; defaults % to [160 300]. % 'InitialValue' vector of indices of which items of the list box % are initially selected; defaults to the first item. % 'Name' String for the figure's title. Defaults to ''. % 'PromptString' string matrix or cell array of strings which appears % as text above the list box. Defaults to {}. % 'OKString' string for the OK button; defaults to 'OK'. % 'CancelString' string for the Cancel button; defaults to 'Cancel'. % 'uh' uicontrol button height, in pixels; default = 18. % 'fus' frame/uicontrol spacing, in pixels; default = 8. % 'ffs' frame/figure spacing, in pixels; default = 8. % % A 'Select all' button is provided in the multiple selection case. % % Example: % d = dir; % str = {d.name}; % [s,v] = listdlg('PromptString','Select a file:',... % 'SelectionMode','single',... % 'ListString',str) % Copyright 2000-2010 The MathWorks, Inc. and Ford Global Technologies, Inc. % if first input is an integer, dispatch to callbacks: % listdlg(0) - OK button callback % listdlg(1) - cancel button callback % listdlg(2) - select all button callback % listdlg(4) - double-click to choose the entry (Addition by PNS) if ischar(varargin{1}) figname = ''; smode = 2; % (multiple) promptstring = {}; listsize = [160 300]; initialvalue = []; okstring = 'OK'; cancelstring = 'Cancel'; fus = 8; ffs = 8; uh = 25; for i=1:2:length(varargin) switch lower(varargin{i}) case 'name' figname = varargin{i+1}; case 'promptstring' promptstring = varargin{i+1}; case 'selectionmode' switch lower(varargin{i+1}) case 'single' smode = 1; case 'multiple' smode = 2; end case 'listsize' listsize = varargin{i+1}; case 'liststring' liststring = varargin{i+1}; case 'initialvalue' initialvalue = varargin{i+1}; case 'uh' uh = varargin{i+1}; case 'fus' fus = varargin{i+1}; case 'ffs' ffs = varargin{i+1}; case 'okstring' okstring = varargin{i+1}; case 'cancelstring' cancelstring = varargin{i+1}; end end if ischar(promptstring) promptstring = cellstr(promptstring); end if isempty(initialvalue) initialvalue = 1; end figh = xregdialog('name', figname, 'resize', 'off'); % height extent per line of uicontrol text, in pixels (approximate) ex = get(mbcgui.hgclassesutil.toNative(figh),'DefaultUicontrolFontSize')*1.5; fp = get(0,'DefaultFigurePosition'); w = 2*(fus+ffs)+listsize(1); h = 2*ffs+6*fus+ex*length(promptstring)+listsize(2)+uh+(smode==2)*(fus+uh); fp = [fp(1) fp(2)+fp(4)-h w h]; % keep upper left corner fixed set(figh, 'Position', fp) ok_btn = uicontrol('Parent', figh,... 'Style','pushbutton',... 'String',okstring,... 'Callback','mv_listdlg(0)'); cancel_btn = uicontrol('Parent', figh, ... 'Style','pushbutton',... 'String',cancelstring,... 'Callback','mv_listdlg(1)'); frmtop = mbcgui.container.layoutpanel(... 'Parent', figh, ... 'BorderType', 'etchedin'); liststring=cellstr(liststring); listbox = uicontrol('Parent', frmtop, ... 'Style','listbox',... 'String',liststring,... 'BackgroundColor','w',... 'Max',smode,... 'Tag','listbox',... 'Value',initialvalue, ... 'Callback', 'mv_listdlg(4)'); obj=listbox; if ~isempty(promptstring) prompt_text = uicontrol('Parent', frmtop,'Style','text','String',promptstring,... 'HorizontalAlignment','left','Units','pixels',... 'Position',[ffs+fus fp(4)-(ffs+fus+ex*length(promptstring)) ... listsize(1) ex*length(promptstring)]); ll=xreglayerlayout(frmtop,'border',[0 fus 0 0],... 'elements',{prompt_text}); obj=xregborderlayout(frmtop,'center',obj,'north',ll,... 'innerborder',[0 0 0 ex*length(promptstring)+fus]); end if smode == 2 selectall_btn = uicontrol('Parent', frmtop,... 'Style','pushbutton',... 'String','Select all',... 'Tag','selectall_btn',... 'Callback','mv_listdlg(2)'); if length(initialvalue) == length(liststring) set(selectall_btn,'Enable','off') end ll=xreglayerlayout(frmtop,'border',[0 0 0 fus],... 'elements',{selectall_btn}); set(listbox,'Callback','mv_listdlg(3); mv_listdlg(4)'); obj=xregborderlayout(frmtop,'center',obj,'south',ll,... 'innerborder',[0 uh+fus 0 0]); end set(frmtop, ... 'LayoutBorder', [fus fus fus fus], ... 'LayoutComponent', {obj}); lytmain = xreggridbaglayout(figh, ... 'dimension', [2 4], ... 'rowsizes', [-1 25], ... 'gapx', fus, ... 'gapy', 2*fus, ... 'colsizes', [-1 65 65 -1], ... 'mergeblock', {[1 1], [1 4]}, ... 'border', [ffs ffs ffs ffs], ... 'elements', {frmtop, [], [], ok_btn, [], cancel_btn}); figh.LayoutManager = lytmain; set(lytmain, 'packstatus', 'on'); figh.showDialog(ok_btn); if strcmp(get(figh,'UserData'), 'ok') value = 1; selection = get(listbox,'Value'); else value = 0; selection = []; end delete(figh) else switch varargin{1} case 0 % OK button callback set(gcbf,'UserData','ok', 'Visible', 'off') case 1 % cancel button callback set(gcbf,'UserData','cancel', 'Visible', 'off') case 2 % select all button callback listbox = findobj(gcbf,'Tag','listbox'); selectall_btn = findobj(gcbf,'Tag','selectall_btn'); set(selectall_btn,'Enable','off') s = get(listbox,'String'); set(listbox,'Value',1:length(s)); case 3 % listbox callback listbox = findobj(gcbf,'Tag','listbox'); selectall_btn = findobj(gcbf,'Tag','selectall_btn'); v = get(listbox,'Value'); s = get(listbox,'String'); if length(s)==length(v) set(selectall_btn,'Enable','off') else set(selectall_btn,'Enable','on') end case 4 % Double-click to choose stype = get(gcbf, 'SelectionType'); if (strcmp(stype, 'open')), set(gcbf, 'UserData', 'ok', 'Visible', 'off'); end end end