PageRenderTime 70ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/ATF2/FlightSim/testApps/extDispersion/questdlg.m

http://atf2flightsim.googlecode.com/
MATLAB | 419 lines | 262 code | 64 blank | 93 comment | 40 complexity | f306b8ef0e5f6a91bbe5cbb32bbbf775 MD5 | raw file
Possible License(s): BSD-2-Clause, LGPL-2.0, IPL-1.0, BSD-3-Clause
  1. function ButtonName=questdlg(Question,Title,Btn1,Btn2,Btn3,Default)
  2. %QUESTDLG Question dialog box.
  3. % ButtonName = QUESTDLG(Question) creates a modal dialog box that
  4. % automatically wraps the cell array or string (vector or matrix)
  5. % Question to fit an appropriately sized window. The name of the
  6. % button that is pressed is returned in ButtonName. The Title of
  7. % the figure may be specified by adding a second string argument:
  8. %
  9. % ButtonName = questdlg(Question, Title)
  10. %
  11. % Question will be interpreted as a normal string.
  12. %
  13. % QUESTDLG uses UIWAIT to suspend execution until the user responds.
  14. %
  15. % The default set of buttons names for QUESTDLG are 'Yes','No' and
  16. % 'Cancel'. The default answer for the above calling syntax is 'Yes'.
  17. % This can be changed by adding a third argument which specifies the
  18. % default Button:
  19. %
  20. % ButtonName = questdlg(Question, Title, 'No')
  21. %
  22. % Up to 3 custom button names may be specified by entering
  23. % the button string name(s) as additional arguments to the function
  24. % call. If custom button names are entered, the default button
  25. % must be specified by adding an extra argument, DEFAULT, and
  26. % setting DEFAULT to the same string name as the button you want
  27. % to use as the default button:
  28. %
  29. % ButtonName = questdlg(Question, Title, Btn1, Btn2, DEFAULT);
  30. %
  31. % where DEFAULT is set to Btn1. This makes Btn1 the default answer.
  32. % If the DEFAULT string does not match any of the button string names,
  33. % a warning message is displayed.
  34. %
  35. % To use TeX interpretation for the Question string, a data
  36. % structure must be used for the last argument, i.e.
  37. %
  38. % ButtonName = questdlg(Question, Title, Btn1, Btn2, OPTIONS);
  39. %
  40. % The OPTIONS structure must include the fields Default and Interpreter.
  41. % Interpreter may be 'none' or 'tex' and Default is the default button
  42. % name to be used.
  43. %
  44. % If the dialog is closed without a valid selection, the return value
  45. % is empty.
  46. %
  47. % Example:
  48. %
  49. % ButtonName = questdlg('What is your favorite color?', ...
  50. % 'Color Question', ...
  51. % 'Red', 'Green', 'Blue', 'Green');
  52. % switch ButtonName,
  53. % case 'Red',
  54. % disp('Your favorite color is Red');
  55. % case 'Blue',
  56. % disp('Your favorite color is Blue.')
  57. % case 'Green',
  58. % disp('Your favorite color is Green.');
  59. % end % switch
  60. %
  61. % See also DIALOG, ERRORDLG, HELPDLG, INPUTDLG, LISTDLG,
  62. % MSGBOX, WARNDLG, FIGURE, TEXTWRAP, UIWAIT, UIRESUME.
  63. % Copyright 1984-2007 The MathWorks, Inc.
  64. % $Revision: 5.55.4.14 $
  65. % 2009-12-01, Mark Woodley
  66. % - use Monospaced font rather than the default
  67. if nargin<1
  68. error('MATLAB:questdlg:TooFewArguments', 'Too few arguments for QUESTDLG');
  69. end
  70. Interpreter='none';
  71. if ~iscell(Question),Question=cellstr(Question);end
  72. %%%%%%%%%%%%%%%%%%%%%
  73. %%% General Info. %%%
  74. %%%%%%%%%%%%%%%%%%%%%
  75. Black =[0 0 0 ]/255;
  76. % LightGray =[192 192 192 ]/255;
  77. % LightGray2 =[160 160 164 ]/255;
  78. % MediumGray =[128 128 128 ]/255;
  79. % White =[255 255 255 ]/255;
  80. %%%%%%%%%%%%%%%%%%%%
  81. %%% Nargin Check %%%
  82. %%%%%%%%%%%%%%%%%%%%
  83. if nargout>1
  84. error('MATLAB:questdlg:WrongNumberOutputs', 'Wrong number of output arguments for QUESTDLG');
  85. end
  86. if nargin==1,Title=' ';end
  87. if nargin<=2, Default='Yes';end
  88. if nargin==3, Default=Btn1 ;end
  89. if nargin<=3, Btn1='Yes'; Btn2='No'; Btn3='Cancel';NumButtons=3;end
  90. if nargin==4, Default=Btn2;Btn2=[];Btn3=[];NumButtons=1;end
  91. if nargin==5, Default=Btn3;Btn3=[];NumButtons=2;end
  92. if nargin==6, NumButtons=3;end
  93. if nargin>6
  94. error('MATLAB:questdlg:TooManyInputs', 'Too many input arguments');NumButtons=3; %#ok
  95. end
  96. if isstruct(Default),
  97. Interpreter=Default.Interpreter;
  98. Default=Default.Default;
  99. end
  100. %%%%%%%%%%%%%%%%%%%%%%%
  101. %%% Create QuestFig %%%
  102. %%%%%%%%%%%%%%%%%%%%%%%
  103. FigPos = get(0,'DefaultFigurePosition');
  104. FigPos(3) = 267;
  105. FigPos(4) = 70;
  106. FigPos = getnicedialoglocation(FigPos, get(0,'DefaultFigureUnits'));
  107. QuestFig=dialog( ...
  108. 'Visible' ,'off' , ...
  109. 'Name' ,Title , ...
  110. 'Pointer' ,'arrow' , ...
  111. 'Position' ,FigPos , ...
  112. 'KeyPressFcn' ,@doFigureKeyPress , ...
  113. 'IntegerHandle' ,'off' , ...
  114. 'WindowStyle' ,'normal' , ...
  115. 'HandleVisibility','callback' , ...
  116. 'CloseRequestFcn' ,@doDelete , ...
  117. 'Tag' ,Title ...
  118. );
  119. %%%%%%%%%%%%%%%%%%%%%
  120. %%% Set Positions %%%
  121. %%%%%%%%%%%%%%%%%%%%%
  122. DefOffset =10;
  123. IconWidth =54;
  124. IconHeight =54;
  125. IconXOffset=DefOffset;
  126. IconYOffset=FigPos(4)-DefOffset-IconHeight; %#ok
  127. IconCMap=[Black;get(QuestFig,'Color')]; %#ok
  128. DefBtnWidth =56;
  129. BtnHeight =22;
  130. BtnYOffset=DefOffset;
  131. BtnWidth=DefBtnWidth;
  132. ExtControl=uicontrol(QuestFig , ...
  133. 'Style' ,'pushbutton', ...
  134. 'String' ,' ' ...
  135. );
  136. btnMargin=1.4;
  137. set(ExtControl,'String',Btn1);
  138. BtnExtent=get(ExtControl,'Extent');
  139. BtnWidth=max(BtnWidth,BtnExtent(3)+8);
  140. if NumButtons > 1
  141. set(ExtControl,'String',Btn2);
  142. BtnExtent=get(ExtControl,'Extent');
  143. BtnWidth=max(BtnWidth,BtnExtent(3)+8);
  144. if NumButtons > 2
  145. set(ExtControl,'String',Btn3);
  146. BtnExtent=get(ExtControl,'Extent');
  147. BtnWidth=max(BtnWidth,BtnExtent(3)*btnMargin);
  148. end
  149. end
  150. BtnHeight = max(BtnHeight,BtnExtent(4)*btnMargin);
  151. delete(ExtControl);
  152. MsgTxtXOffset=IconXOffset+IconWidth;
  153. FigPos(3)=max(FigPos(3),MsgTxtXOffset+NumButtons*(BtnWidth+2*DefOffset));
  154. set(QuestFig,'Position',FigPos);
  155. BtnXOffset=zeros(NumButtons,1);
  156. if NumButtons==1,
  157. BtnXOffset=(FigPos(3)-BtnWidth)/2;
  158. elseif NumButtons==2,
  159. BtnXOffset=[MsgTxtXOffset
  160. FigPos(3)-DefOffset-BtnWidth];
  161. elseif NumButtons==3,
  162. BtnXOffset=[MsgTxtXOffset
  163. 0
  164. FigPos(3)-DefOffset-BtnWidth];
  165. BtnXOffset(2)=(BtnXOffset(1)+BtnXOffset(3))/2;
  166. end
  167. MsgTxtYOffset=DefOffset+BtnYOffset+BtnHeight;
  168. MsgTxtWidth=FigPos(3)-DefOffset-MsgTxtXOffset-IconWidth;
  169. MsgTxtHeight=FigPos(4)-DefOffset-MsgTxtYOffset;
  170. MsgTxtForeClr=Black;
  171. MsgTxtBackClr=get(QuestFig,'Color');
  172. CBString='uiresume(gcbf)';
  173. DefaultValid = false;
  174. DefaultWasPressed = false;
  175. BtnHandle = [];
  176. DefaultButton = 0;
  177. % Check to see if the Default string passed does match one of the
  178. % strings on the buttons in the dialog. If not, throw a warning.
  179. for i = 1:NumButtons
  180. switch i
  181. case 1
  182. ButtonString=Btn1;
  183. ButtonTag='Btn1';
  184. if strcmp(ButtonString, Default)
  185. DefaultValid = true;
  186. DefaultButton = 1;
  187. end
  188. case 2
  189. ButtonString=Btn2;
  190. ButtonTag='Btn2';
  191. if strcmp(ButtonString, Default)
  192. DefaultValid = true;
  193. DefaultButton = 2;
  194. end
  195. case 3
  196. ButtonString=Btn3;
  197. ButtonTag='Btn3';
  198. if strcmp(ButtonString, Default)
  199. DefaultValid = true;
  200. DefaultButton = 3;
  201. end
  202. end
  203. BtnHandle(end+1)=uicontrol(QuestFig , ...
  204. 'Style' ,'pushbutton', ...
  205. 'Position' ,[ BtnXOffset(1) BtnYOffset BtnWidth BtnHeight ] , ...
  206. 'KeyPressFcn' ,@doControlKeyPress , ...
  207. 'CallBack' ,CBString , ...
  208. 'String' ,ButtonString, ...
  209. 'HorizontalAlignment','center' , ...
  210. 'Tag' ,ButtonTag ...
  211. );
  212. end
  213. if ~DefaultValid
  214. warnstate = warning('backtrace','off');
  215. warning('MATLAB:QUESTDLG:stringMismatch','Default string does not match any button string name.');
  216. warning(warnstate);
  217. end
  218. MsgHandle=uicontrol(QuestFig , ...
  219. 'Style' ,'text' , ...
  220. 'Position' ,[MsgTxtXOffset MsgTxtYOffset 0.95*MsgTxtWidth MsgTxtHeight ] , ...
  221. 'String' ,{' '} , ...
  222. 'Tag' ,'Question' , ...
  223. 'HorizontalAlignment','left' , ...
  224. 'FontWeight' ,'bold' , ...
  225. 'BackgroundColor' ,MsgTxtBackClr , ...
  226. 'ForegroundColor' ,MsgTxtForeClr ...
  227. );
  228. [WrapString,NewMsgTxtPos]=textwrap(MsgHandle,Question,75);
  229. % NumLines=size(WrapString,1);
  230. AxesHandle=axes('Parent',QuestFig,'Position',[0 0 1 1],'Visible','off');
  231. texthandle=text( ...
  232. 'Parent' ,AxesHandle , ...
  233. 'Units' ,'pixels' , ...
  234. 'Color' ,get(BtnHandle(1),'ForegroundColor') , ...
  235. 'HorizontalAlignment' ,'left' , ...
  236. 'FontName' ,'Monospaced' , ...
  237. 'FontSize' ,get(BtnHandle(1),'FontSize') , ...
  238. 'VerticalAlignment' ,'bottom' , ...
  239. 'String' ,WrapString , ...
  240. 'Interpreter' ,Interpreter , ...
  241. 'Tag' ,'Question' ...
  242. ); %#ok
  243. textExtent = get(texthandle, 'extent');
  244. % (g357851)textExtent and extent from uicontrol are not the same. For window, extent from uicontrol is larger
  245. %than textExtent. But on Mac, it is reverse. Pick the max value.
  246. MsgTxtWidth=max([MsgTxtWidth NewMsgTxtPos(3)+2 textExtent(3)]);
  247. MsgTxtHeight=max([MsgTxtHeight NewMsgTxtPos(4)+2 textExtent(4)]);
  248. MsgTxtXOffset=IconXOffset+IconWidth+DefOffset;
  249. FigPos(3)=max(NumButtons*(BtnWidth+DefOffset)+DefOffset, ...
  250. MsgTxtXOffset+MsgTxtWidth+DefOffset);
  251. % Center Vertically around icon
  252. if IconHeight>MsgTxtHeight,
  253. IconYOffset=BtnYOffset+BtnHeight+DefOffset;
  254. MsgTxtYOffset=IconYOffset+(IconHeight-MsgTxtHeight)/2;
  255. FigPos(4)=IconYOffset+IconHeight+DefOffset;
  256. % center around text
  257. else
  258. MsgTxtYOffset=BtnYOffset+BtnHeight+DefOffset;
  259. IconYOffset=MsgTxtYOffset+(MsgTxtHeight-IconHeight)/2;
  260. FigPos(4)=MsgTxtYOffset+MsgTxtHeight+DefOffset;
  261. end
  262. if NumButtons==1,
  263. BtnXOffset=(FigPos(3)-BtnWidth)/2;
  264. elseif NumButtons==2,
  265. BtnXOffset=[(FigPos(3)-DefOffset)/2-BtnWidth
  266. (FigPos(3)+DefOffset)/2
  267. ];
  268. elseif NumButtons==3,
  269. BtnXOffset(2)=(FigPos(3)-BtnWidth)/2;
  270. BtnXOffset=[BtnXOffset(2)-DefOffset-BtnWidth
  271. BtnXOffset(2)
  272. BtnXOffset(2)+BtnWidth+DefOffset
  273. ];
  274. end
  275. set(QuestFig ,'Position',getnicedialoglocation(FigPos, get(QuestFig,'Units')));
  276. BtnPos=get(BtnHandle,{'Position'});
  277. BtnPos=cat(1,BtnPos{:});
  278. BtnPos(:,1)=BtnXOffset;
  279. BtnPos=num2cell(BtnPos,2);
  280. set(BtnHandle,{'Position'},BtnPos);
  281. if DefaultValid
  282. setdefaultbutton(QuestFig, BtnHandle(DefaultButton));
  283. end
  284. delete(MsgHandle);
  285. set(texthandle, 'Position',[MsgTxtXOffset MsgTxtYOffset 0]);
  286. IconAxes=axes( ...
  287. 'Parent' ,QuestFig , ...
  288. 'Units' ,'Pixels' , ...
  289. 'Position' ,[IconXOffset IconYOffset IconWidth IconHeight], ...
  290. 'NextPlot' ,'replace' , ...
  291. 'Tag' ,'IconAxes' ...
  292. );
  293. set(QuestFig ,'NextPlot','add');
  294. load dialogicons.mat questIconData questIconMap;
  295. IconData=questIconData;
  296. questIconMap(256,:)=get(QuestFig,'color');
  297. IconCMap=questIconMap;
  298. Img=image('CData',IconData,'Parent',IconAxes);
  299. set(QuestFig, 'Colormap', IconCMap);
  300. set(IconAxes, ...
  301. 'Visible','off' , ...
  302. 'YDir' ,'reverse' , ...
  303. 'XLim' ,get(Img,'XData'), ...
  304. 'YLim' ,get(Img,'YData') ...
  305. );
  306. % make sure we are on screen
  307. movegui(QuestFig)
  308. set(QuestFig ,'WindowStyle','modal','Visible','on');
  309. drawnow;
  310. if DefaultButton ~= 0
  311. uicontrol(BtnHandle(DefaultButton));
  312. end
  313. if ishghandle(QuestFig)
  314. % Go into uiwait if the figure handle is still valid.
  315. % This is mostly the case during regular use.
  316. uiwait(QuestFig);
  317. end
  318. % Check handle validity again since we may be out of uiwait because the
  319. % figure was deleted.
  320. if ishghandle(QuestFig)
  321. if DefaultWasPressed
  322. ButtonName=Default;
  323. else
  324. ButtonName=get(get(QuestFig,'CurrentObject'),'String');
  325. end
  326. doDelete;
  327. else
  328. ButtonName='';
  329. end
  330. function doFigureKeyPress(obj, evd) %#ok
  331. switch(evd.Key)
  332. case {'return','space'}
  333. if DefaultValid
  334. DefaultWasPressed = true;
  335. uiresume(gcbf);
  336. end
  337. case 'escape'
  338. doDelete
  339. end
  340. end
  341. function doControlKeyPress(obj, evd) %#ok
  342. switch(evd.Key)
  343. case {'return'}
  344. if DefaultValid
  345. DefaultWasPressed = true;
  346. uiresume(gcbf);
  347. end
  348. case 'escape'
  349. doDelete
  350. end
  351. end
  352. function doDelete(varargin) %#ok
  353. delete(QuestFig);
  354. end
  355. end