Home > wafo > misc > choices.m

choices

PURPOSE ^

Create a list of choices with uicontrols and callbacks.

SYNOPSIS ^

choices(name,header,labels,callbacks,inter)

DESCRIPTION ^

 CHOICES Create a list of choices with uicontrols and callbacks. 
    CHOICES('NAME',HEADER,BUTTONLABELS,CALLBACKS) creates 
    a window with registered name NAME.  The window contains 
    the string HEADER and buttons labeled with BUTTONLABELS. 
    These buttons register callback strings from CALLBACKS. 
    An additional button, labeled 'Close', is added to each 
    choicelist. 
  
    CHOICES is useful for constructing demo menus. 
    Use CHOICES in conjunction with CHOICEX, as in 
    this example. 
        header = 'Easy Example'; 
        labels = str2mat('Choice 1','Choice 2','Choice 3'); 
        callbacks = str2mat('image(magic(1))','image(magic(2))', ... 
            'image(magic(3))'); 
        choices('EXAMPLE', header, labels, callbacks); 
  
    A final, optional, non-zero argument for CHOICES causes the 
    buttons to have their interruptible property set to 'on'.  This 
    is necessary for any demos requiring any mouse button presses, for 
    selections of options or data, for example.

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

001 function choices(name,header,labels,callbacks,inter) 
002 %CHOICES Create a list of choices with uicontrols and callbacks. 
003 %   CHOICES('NAME',HEADER,BUTTONLABELS,CALLBACKS) creates 
004 %   a window with registered name NAME.  The window contains 
005 %   the string HEADER and buttons labeled with BUTTONLABELS. 
006 %   These buttons register callback strings from CALLBACKS. 
007 %   An additional button, labeled 'Close', is added to each 
008 %   choicelist. 
009 % 
010 %   CHOICES is useful for constructing demo menus. 
011 %   Use CHOICES in conjunction with CHOICEX, as in 
012 %   this example. 
013 %       header = 'Easy Example'; 
014 %       labels = str2mat('Choice 1','Choice 2','Choice 3'); 
015 %       callbacks = str2mat('image(magic(1))','image(magic(2))', ... 
016 %           'image(magic(3))'); 
017 %       choices('EXAMPLE', header, labels, callbacks); 
018 % 
019 %   A final, optional, non-zero argument for CHOICES causes the 
020 %   buttons to have their interruptible property set to 'on'.  This 
021 %   is necessary for any demos requiring any mouse button presses, for 
022 %   selections of options or data, for example. 
023  
024 %%%   Loren Shure, 8-14-92. 
025 %%%   Copyright 1984-2000 The MathWorks, Inc. 
026 %%%   $Revision: 5.16 $  $Date: 2000/06/01 03:46:32 $ 
027  
028 global CHOICELIST 
029 global CHOICEHANDLES 
030 c = computer; 
031 if ~isstr(name) | ~isstr(header) | ~isstr(labels) | ~isstr(callbacks) 
032    error('Requires string arguments.'); 
033 end 
034 if nargin < 4 
035    error('Not enough input arguments.') 
036 end 
037 if nargin == 4 
038    inter = 0; 
039 end 
040 if inter 
041    yn = 'on'; 
042 else 
043    yn = 'off'; 
044 end 
045 uicok = strcmp(c(1:2),'PC') | strcmp(c(1:2),'MA'); 
046 if isunix | ~uicok 
047    uicok = strcmp(lower(get(0,'TerminalProtocol')),'x'); 
048 end 
049 %can't use uicontrols -use menu stuff instead- this is for terminals -UNIX & VMS 
050 if ~uicok 
051    labels = str2mat(labels,'Done'); 
052    nl = size(labels,1); 
053    % build up menu string for evaluation 
054    % fix quotes, if there are any 
055    ss = deblank(labels(1,:)); 
056    ss = ss(sort([1:length(ss) find(ss=='''')])); 
057    args = ['''',ss,'''']; 
058    header = header(sort([1:length(header) find(header=='''')])); 
059    for i = 2:nl 
060       ss = deblank(labels(i,:)); 
061       ss = ss(sort([1:length(ss) find(ss=='''')])); 
062       args = [args, ',''', ss,'''']; 
063    end 
064    k = 1; 
065    while k > 0 & k < nl 
066       k = eval(['menu(''',header,''',', args,');']); 
067       if k == nl | k == 0 
068          return 
069       else 
070          ceval(callbacks(k,:)); 
071       end 
072    end 
073    return 
074 end 
075 % can use uicontrols 
076 name = deblank(name); 
077 if isempty(name) 
078    error('Requires non-blank string argument.') 
079 end 
080 % ensure list doesn't go into figure 1 
081 figs = sort(get(0,'Children')); 
082 openfigs = size(figs); 
083 if ~isempty(figs) 
084    cf = gcf; 
085    if cf == 1 
086       cf = []; 
087    end 
088 else 
089    cf = []; 
090 end 
091 fig1 = 1; 
092 if isempty(figs) 
093    CHOICELIST = []; 
094    CHOICEHANDLES = []; 
095    figs = figure('visible','off'); 
096    fig1 = 0; 
097 end 
098 if figs(1) ~= 1 
099    figs = [figure('visible','off'); figs]; 
100    fig1 = 0; 
101 end 
102 matchn = 0; 
103 for i = 1:size(CHOICELIST,1) 
104    if strcmp(name,deblank(CHOICELIST(i,:))) 
105       matchn = i; 
106       break; 
107    end 
108 end 
109 if ~matchn 
110    CHOICEHANDLES = [CHOICEHANDLES(:); 0]; 
111    if isempty(CHOICELIST) 
112       CHOICELIST = name; 
113    else 
114       CHOICELIST = str2mat(CHOICELIST, name); 
115    end 
116    matchn = size(CHOICEHANDLES,1); 
117 else 
118    matchh = 0; 
119    for i = 1:size(figs,1) 
120        if figs(i) == CHOICEHANDLES(matchn) 
121           matchh = i; 
122           break; 
123        end 
124    end 
125    if matchh 
126        figure(CHOICEHANDLES(matchn)); 
127        return 
128    end 
129 end 
130 ss = get(0,'ScreenSize'); 
131 xedge = 30; 
132 ybord = 30; 
133 width = 30; 
134 yedge = 35; 
135  
136 % Determine size of text labels 
137  
138 ha = axes('visible','off'); 
139 %hh = text(.1,.1,labels,'units','pixel') 
140 hh = text(ones(size(labels,1)+1,1),ones(size(labels,1)+1,1),str2mat(labels,'Close'),'units','pixel'); 
141 maxwidth = 0; 
142 height = 0; 
143 for i = 1:length(hh), 
144     ext = get(hh(i),'extent'); 
145     maxwidth = max(maxwidth,ext(3)); 
146     height = max(height,ext(4)); 
147 end       
148 delete(hh);delete(ha); 
149 yedge = 1.5*height; 
150 height = 6*yedge/7; 
151  
152 imax = 1; 
153  
154 twidth = maxwidth; 
155 % now figure out total dimensions needed so things can get placed in pixels 
156 mwwidth = twidth + width + 2*xedge; 
157 mwheight = (size(labels,1)+2.5)*yedge; 
158 swidth = ss(3); sheight = ss(4); 
159 left = 20; 
160 bottom = sheight-mwheight-ybord; 
161 rect = [left bottom mwwidth mwheight]; 
162 CHOICEHANDLES(matchn) = figure('Position',rect,'number','off', ... 
163        'name','','resize','off','colormap',[],... 
164        'Menubar','none','visible','off'); 
165  
166 fg = CHOICEHANDLES(matchn); 
167 fgs = CHOICEHANDLES(CHOICEHANDLES ~= fg); 
168 set(fgs,'visible','off') 
169 set(gca,'Position',[0 0 1 1]); axis off; 
170  
171 % Place header 
172  
173 hdrpos = [.05 1-1/(size(labels,1)+1.6) .9 1/(size(labels,1)+1.6)]; 
174  
175 hh=uicontrol(fg,'style','text','units','normal',... 
176            'position',hdrpos,'string',header,... 
177            'Horizontal','center'); 
178 set(hh,'backg',get(gcf,'color'),'foreg',[1 1 1]-get(gcf,'color')) 
179 % set up pre-amble so figure 1 is available for rendering in 
180 sb = ['figure(1),set(1,''visible'',''on'');set(',int2str(fg),',''visible'',''off'');']; 
181 se = [';global CHOICEHANDLES;set(CHOICEHANDLES(length(CHOICEHANDLES)),''visible'',''on''),clear CHOICEHANDLES']; 
182 for ii=size(labels,1):-1:1 
183     i = size(labels,1) + 2 - ii; 
184     h1 = uicontrol(fg,'position',[xedge  (i-.5)*yedge width+twidth height],... 
185          'callback',[sb callbacks(ii,:) se],... 
186          'string',['  ',deblank(labels(ii,:)), '  '],... 
187          'HorizontalAlignment','left','interruptible',yn); 
188 % left justify string inside button 
189 end 
190 % Create Close button 
191 uicontrol(fg,'position',[xedge .5*yedge width+twidth height],... 
192       'string','  Close  ',... 
193       'callback',['choicex(''',name,''')'],... 
194       'HorizontalAlignment','left'); 
195  
196 set(fg,'HandleVisibility','callback','visible','on') 
197  
198 if ~isempty(cf) 
199    figure(cf) 
200 else 
201    if ~fig1 
202       close(1); 
203    end 
204 end 
205  
206

Mathematical Statistics
Centre for Mathematical Sciences
Lund University with Lund Institute of Technology

Comments or corrections to the WAFO group


Generated on Thu 06-Oct-2005 02:21:16 for WAFO by m2html © 2003