home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l460 / 3.ddi / DEMOS.DI$ / CHOICES.M < prev    next >
Encoding:
Text File  |  1993-03-07  |  4.9 KB  |  169 lines

  1. function choices(name,header,labels,callbacks)
  2. %CHOICES Create a list of choices with uicontrols and callbacks.
  3. %    CHOICES('NAME',HEADER,BUTTONLABELS,CALLBACKS) creates
  4. %    a window with registered name NAME.  The window contains
  5. %    the string HEADER and buttons labeled with BUTTONLABELS.
  6. %    These buttons register callback strings from CALLBACKS.
  7. %    An additional button, labeled 'Close', is added to each
  8. %    choicelist.
  9. %
  10. %    CHOICES is useful for constructing demo menus.
  11. %    Use CHOICES in conjunction with CHOICEX, as in
  12. %    this example.
  13. %        header = 'Easy Example';
  14. %        labels = str2mat('Choice 1','Choice 2','Choice 3');
  15. %        callbacks = str2mat('image(magic(1))','image(magic(2))', ...
  16. %            'image(magic(3))');
  17. %        choices('EXAMPLE', header, labels, callbacks);
  18. %
  19.  
  20. %    Loren Shure, 8-14-92.
  21. %    Copyright (c) 1984-93 by The MathWorks, Inc.
  22.  
  23. c = computer;
  24. if ~isstr(name) | ~isstr(header) | ~isstr(labels) | ~isstr(callbacks)
  25.     error('Requires string arguments.');
  26. end
  27. if nargin < 4
  28.     error('Not enough input arguments.')
  29. end
  30. uicok = strcmp(c(1:2),'PC') | strcmp(c(1:2),'MA');
  31. if isunix | ~uicok   % could be VMS as well
  32.     uicok = strcmp(lower(get(0,'TerminalProtocol')),'x');
  33. end
  34. %can't use uicontrols -use menu stuff instead- this is for terminals -UNIX & VMS
  35. if ~uicok
  36.    labels = str2mat(labels,'Done');
  37.    nl = size(labels,1);
  38.    % build up menu string for evaluation
  39.    % fix quotes, if there are any
  40.    ss = deblank(labels(1,:));
  41.    ss = ss(sort([1:length(ss) find(ss=='''')]));
  42.    args = ['''',ss,''''];
  43.    header = header(sort([1:length(header) find(header=='''')]));
  44.    for i = 2:nl
  45.       ss = deblank(labels(i,:));
  46.       ss = ss(sort([1:length(ss) find(ss=='''')]));
  47.       args = [args, ',''', ss,''''];
  48.    end
  49.    k = 1;
  50.    while k > 0 & k < nl
  51.       eval(['k = menu(''',header,''',', args,');']);
  52.       if k == nl | k == 0
  53.          return
  54.       else
  55.          eval(callbacks(k,:));
  56.       end
  57.    end
  58. end
  59. % can use uicontrols
  60. global CHOICELIST
  61. global CHOICEHANDLES
  62. name = deblank(name);
  63. if isempty(name)
  64.     error('Requires non-blank string argument.')
  65. end
  66. % ensure list doesn't go into figure 1
  67. figs = sort(get(0,'Children'));
  68. openfigs = size(figs);
  69. if ~isempty(figs)
  70.    cf = gcf;
  71.    if cf == 1
  72.       cf = [];
  73.    end
  74. else
  75.    cf = [];
  76. end
  77. fig1 = 1;
  78. if isempty(figs)
  79.     figs = figure('visible','off');
  80.         fig1 = 0;
  81. end
  82. if figs(1) ~= 1
  83.     figs = [figure('visible','off'); figs];
  84.         fig1 = 0;
  85. end
  86. matchn = 0;
  87. for i = 1:size(CHOICELIST,1)
  88.     if strcmp(name,deblank(CHOICELIST(i,:)))
  89.         matchn = i;
  90.         break;
  91.     end
  92. end
  93. if ~matchn
  94.     CHOICEHANDLES = [CHOICEHANDLES; 0];
  95.     if isempty(CHOICELIST)
  96.         CHOICELIST = name;
  97.     else
  98.         CHOICELIST = str2mat(CHOICELIST, name);
  99.     end
  100.     matchn = size(CHOICEHANDLES,1);
  101. else
  102.     matchh = 0;
  103.     for i = 1:size(figs,1)
  104.         if figs(i) == CHOICEHANDLES(matchn)
  105.             matchh = i;
  106.             break;
  107.         end
  108.     end
  109.     if matchh
  110.         figure(CHOICEHANDLES(matchn));
  111.         return
  112.     end
  113. end
  114. xedge = 30;
  115. ybord = 30;
  116. width = 30;
  117. yedge = 35;
  118. height = 30;
  119. if c(1:2) == 'PC'
  120.       ha = axes('visible','off');
  121.       hh = text(.1,.1,'Aq','units','pixel');yedge = get(hh,'extent');
  122.       delete(hh);delete(ha);
  123.       yedge = 1.6*yedge(4);
  124.       height = 6*yedge/7;
  125. end
  126. avwidth = 7; % actually 6.8886 +/- 0.4887
  127. imax = 1;
  128. maxlen = size(labels,2);
  129. twidth = 1.2*maxlen*avwidth;
  130. % now figure out total dimensions needed so things can get placed in pixels
  131. mwwidth = twidth + width + 2*xedge;
  132. mwheight = (size(labels,1)+2)*yedge;
  133. ss = get(0,'ScreenSize');
  134. swidth = ss(3); sheight = ss(4);
  135. left = 20;
  136. bottom = sheight-mwheight-ybord;
  137. rect = [left bottom mwwidth mwheight];
  138. CHOICEHANDLES(matchn) = figure('Position',rect,'number','off', ...
  139.        'name','','resize','off','colormap',[],'NextPlot','new',...
  140.        'Menubar','none');
  141. fg = CHOICEHANDLES(matchn);
  142. fgs = CHOICEHANDLES(CHOICEHANDLES ~= fg);
  143. set(fgs,'visible','off')
  144. set(gca,'Position',[0 0 1 1]); axis off;
  145. % Place header
  146. hh=uicontrol(fg,'style','text','units','normal',...
  147.              'position',[.05,.91,.9,.09 ],'string',header,...
  148.              'Horizontal','center');
  149. set(hh,'backg',get(gcf,'color'),'foreg',[1 1 1]-get(gcf,'color'))
  150. % set up pre-amble so figure 1 is available for rendering in
  151. sb = ['figure(1),set(1,''visible'',''on'');set(',int2str(fg),',''visible'',''off'');'];
  152. se = [';global CHOICEHANDLES,set(CHOICEHANDLES(length(CHOICEHANDLES)),''visible'',''on'')'];
  153. for ii=size(labels,1):-1:1
  154.     i = size(labels,1) + 2 - ii;
  155.     h1 = uicontrol(fg,'position',[xedge  (i-.5)*yedge width+twidth height]);
  156.     set(h1,'callback',[sb callbacks(ii,:) se])
  157.     set(h1,'string',['  ',deblank(labels(ii,:)), '  ']);
  158. % left justify string inside button
  159.     set(h1,'HorizontalAlignment','left');
  160. end
  161. % Create Close button
  162. h1 = uicontrol(fg,'position',[xedge .5*yedge width+twidth height]);
  163. set(h1,'string','  Close  ');
  164. set(h1,'callback',['choicex(''',name,''')']);
  165. set(h1,'HorizontalAlignment','left');
  166. if ~isempty(cf)
  167.    figure(cf)
  168. end
  169.