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