-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenableDisableFig.m
166 lines (154 loc) · 5.65 KB
/
enableDisableFig.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
function currentState = enableDisableFig(hFig, newState)
% enableDisableFig enable or disable an entire figure window
%
% Syntax:
% currentState = enableDisableFig(hFig, newState)
%
% Description:
% enableDisableFig sets the figure hFig's enable/disable state, which
% is otherwise supported by Matlab only for specific components but not
% figures. Using this function, the entire figure window, including all
% internal menus, toolbars and components, is enabled/disabled in a
% single call. Valid values for newState are true, false, 'on' & 'off'
% (case insensitive). hFig may be a list of figure handles.
%
% Note 1: when the state is enabled, internal figure components may
% remain disabled if their personal 'enabled' property is 'off'.
%
% Note 2: in disabled state, a figure cannot be moved, resized, closed
% or accessed. None of its menues, toolbars, buttons etc. are clickable.
%
% enableDisableFig(newState) sets the state of the current figure (gcf).
% Note 3: this syntax (without hFig) might cause unexpected results if
% newState is a numeric value (interpreted as a figure handle), instead
% of a string or logical value.
%
% state = enableDisableFig(hFig) returns the current enabled/disabled
% state of figure hFig, or of the current figure (gcf) if hFig is not
% supplied. The returned state is either 'on' or 'off'.
%
% Examples:
% state = enableDisableFig;
% state = enableDisableFig(hFig);
% oldState = enableDisableFig(hFig, 'on');
% oldState = enableDisableFig(hFig, result>0);
% oldState = enableDisableFig(true); % on current figure (Note 3 above)
%
% Bugs and suggestions:
% Please send to Yair Altman (altmany at gmail dot com)
%
% Warning:
% This code heavily relies on undocumented and unsupported Matlab
% functionality. It works on Matlab 7+, but use at your own risk!
%
% Change log:
% 2007-08-10: First version posted on <a href="http://www.mathworks.com/matlabcentral/fileexchange/loadAuthor.do?objectType=author&mfx=1&objectId=1096533#">MathWorks File Exchange</a>
%
% See also:
% gcf, findjobj, getJFrame (last two on the File Exchange)
% Programmed by Yair M. Altman: altmany(at)gmail.com
% $Revision: 1.0 $ $Date: 2007/08/10 18:23:52 $
try
% Default figure = current (gcf)
if nargin < 1 || ~all(ishandle(hFig))
if nargin && ~ishandle(hFig)
if nargin < 2
newState = hFig;
else
error('hFig must be a valid figure handle');
end
end
hFig = gcf;
end
% Require Java engine to run
if ~usejava('jvm')
error([mfilename ' requires Java to run.']);
end
% Loop over all requested figures
for figIdx = 1 : length(hFig)
% Get the root Java frame
jff = getJFrame(hFig(figIdx));
% Get the current frame's state
currentState{figIdx} = get(jff,'Enabled'); %#ok loop
% Set the new figure enabled state, if requested
if exist('newState','var')
if ischar(newState)
newState = lower(newState);
if ~any(strcmp(newState,{'on','off'}))
error('newState must be one of: ''on'', ''off'', true, false')
end
end
try
set(jff,'Enabled',newState); % accepts 'on'/'off'
catch
set(handle(jff),'Enabled',newState); % accepts true/false
end
end
end
% De-cell a single value
if length(currentState) == 1
currentState = currentState{1};
end
% Error handling
catch
v = version;
if v(1)<='6'
err.message = lasterr; % no lasterror function...
else
err = lasterror;
end
try
err.message = regexprep(err.message,'Error using ==> [^\n]+\n','');
catch
try
% Another approach, used in Matlab 6 (where regexprep is unavailable)
startIdx = findstr(err.message,'Error using ==> ');
stopIdx = findstr(err.message,char(10));
for idx = length(startIdx) : -1 : 1
idx2 = min(find(stopIdx > startIdx(idx))); %#ok ML6
err.message(startIdx(idx):stopIdx(idx2)) = [];
end
catch
% never mind...
end
end
if isempty(findstr(mfilename,err.message))
% Indicate error origin, if not already stated within the error message
err.message = [mfilename ': ' err.message];
end
if v(1)<='6'
while err.message(end)==char(10)
err.message(end) = []; % strip excessive Matlab 6 newlines
end
error(err.message);
else
rethrow(err);
end
end
%% Get the root Java frame (up to 5 tries, to wait for figure to become responsive)
function jframe = getJFrame(hFig)
% Ensure that hFig is a figure handle...
hFig = ancestor(hFig,'figure');
jframe = [];
maxTries = 10;
while maxTries > 0
try
% Get the figure's underlying Java frame
jf = get(hFig,'javaframe');
% Get the Java frame's root frame handle
%jframe = jf.getFigurePanelContainer.getComponent(0).getRootPane.getParent;
jframe = jf.fFigureClient.getWindow; % equivalent to above...
if ~isempty(jframe)
break;
else
maxTries = maxTries - 1;
drawnow; pause(0.1);
end
catch
maxTries = maxTries - 1;
drawnow; pause(0.1);
end
end
if isempty(jframe)
error('Cannot retrieve figure''s java frame');
end